Bitmap rasmini Paragrafga Fragmentdan qanday o'tkazish mumkin

Mening faoliyatimda bu kabi bitmap rasmim bor:

Bitmap image4 = BitmapFactory.decodeResource(getResources(), R.drawable.hello);
        image1.setImageBitmap(image4);

Ushbu kodni foydalanib, ushbu rasmni fragmentda o'tkazmoqchiman:

  FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.addToBackStack(null);
                Secondfrag yourFragment = new Secondfrag();
                Bundle b = new Bundle();
                b.putParcelable("BitmapImage",image4);

                yourFragment.setArguments(b);
                fragmentTransaction.add(R.id.frag, yourFragment, "FRAGMENT");
               fragmentTransaction.commit();

va bu tasvirni shu kabi qismga tushirishga harakat qilamiz:

Bundle b=this.getArguments();
        String page=b.getString("BitmapImage");

        image2.setImageURI(Uri.parse(page));

Ammo mening Fragmentimda hech qanday tasvir yo'q. Bu muammoni qanday hal qilish mumkin?

0
@TahmidRahman aytganidek, senga murojaat qilishingiz mumkin :)
qo'shib qo'ydi muallif Ali Gürelli, manba
Ichki xotirada saqlash orqali buni amalga oshirishingiz mumkin.
qo'shib qo'ydi muallif Arjun, manba
Malumotni topshirish yaxshi fikr deb o'ylayman
qo'shib qo'ydi muallif Tahmid Rahman, manba

6 javoblar

Bayt qatoriga aylantirish

ByteArrayOutputStream stream = new ByteArrayOutputStream();
  bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
  byte[] byteArray = stream.toByteArray();

Bundle b = new Bundle();
b.putByteArray("image",byteArray);


 //your fragment code 
fragment.setArguments(b);

Nishon fragmentidan qiymat oling

byte[] byteArray = getArgument().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

Credit to https://stackoverflow.com/a/33797090/4316327

1
qo'shib qo'ydi

Iltimos, ushbu usulni sinab ko'ring

private void nextpageintent(String photoUri){

Bundle bundle = new Bundle();
bundle.putString("photo", photoUri);
picture picture = new picture();
picture .setArguments(bundle); 
FragmentTransaction transaction = null;
transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, picture); 
transaction.addToBackStack(null);
transaction.commit();

}

0
qo'shib qo'ydi

Bundle ichida rasm yo'lini/URI-ni faqatgina yuborishingiz mumkin.

Siz o'zingiz uchun Bitmapni yaratish uchun resurslardan foydalanmoqdasiz. Bitmapni fragmentingizda olish uchun bir xil koddan foydalanishingiz mumkin. Manbalar fragmentda getActivity() sifatida mavjud. GetResources ();

Bitmap image4 = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.hello);
    image1.setImageBitmap(image4);
0
qo'shib qo'ydi

Zeerak Hameem uchun ovoz berish siz turli xil yo'llar bilan o'ylashingiz kerak

0
qo'shib qo'ydi

Bu yerda siz bu savolni Singleton sinfining qabul qiluvchi setteridan foydalanib hal qilishingiz mumkin, Avval bitmap xotirasi uchun bitta Ben sinfini yarating.

class BitmapStorage {
private static volatile BitmapStorage instance = null;
private Bitmap bitmap = null;
private BitmapStorage() {}

    public static BitmapStorage getInstance() {
        if (instance == null) {
            synchronized(BitmapStorage.class) {
                if (instance == null) {
                    instance = new BitmapStorage();
                }
            }
        }
        return instance;
    }

    public Bitmap getBitmap(){
           return bitmap;
    }

    public void setBitmap(Bitmap bitmap){
           this.bitmap = bitmap;
    }
}

So'ngra o'zingizning faoliyatingizdagi bitmapni sizning modelingiz sinfiga qo'ying. Xuddi shunday

BitmapStrorage bs = getInstance ();
Bitmap image4 = BitmapFactory.decodeResource(getResources(), R.drawable.hello);
Bs.setBitmap(image4);

Fragman parcha hayot aylanishiga ko'ra chaqirilsa, OnCreate usuli avval chaqiriladi, shuning uchun biz bu erda bitmapni null yoki yo'qligini tekshiramiz va bitmap olishimiz va uni ishlatishimiz mumkin. Bu echimni tekshirib ko'rmasligingiz mumkin, lekin men siz uchun ishlayapman.

0
qo'shib qo'ydi

Rasmiy ma'lumotlarning asl nusxasi butun rasm ma'lumotlarini o'tqazish o'rniga yaxshi usul.

Sizning holatingizda Uri rasmini berishi mumkin

Agar siz ichki xotiradan rasmni olishni istasangiz, siz Uri kodidan foydalanishingiz mumkin. Agar siz faqat ma'lum bir resursni (ya'ni, drawable ) xohlasangiz, unda u o'z ismini ( string ) o'tkazishi mumkin.

You can check this solution Passing Uri to an Intent

0
qo'shib qo'ydi