如何将位图图像从 Activity 传输到 Fragment

How to transfer bitmap image from Activity to Fragment

我的 Activity 中有一张位图图像,如下所示:

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

我正在尝试使用以下代码片段传递此图像:

  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();

并尝试像这样在片段中获取此图像:

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

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

但是我的Fragment.How没有图片显示解决这个问题?

传递图像引用比传递整个图像数据更好。

在你的情况下,你可以传递你的图像 Uri

如果你想从内部存储中获取图像,你可以通过Uri。但是,如果您只想要一个特定的资源(即 drawable),那么它可以只传递其名称(即 String)。

你可以查看这个解决方案Passing Uri to an Intent

转换成字节数组

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);

从目标片段中获取值

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

归功于

您可以只在捆绑包中传递您的图片 path/URI。

在你的情况下,你正在使用你的资源来制作位图。您可以使用相同的代码来获取片段中的位图。片段中的资源可用 getActivity().getResources();

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

请试试这个方法

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();

}

在这里,这个问题你可以简单地使用单例class的gettersetter来解决, 首先为您的位图存储制作一个 Ben class。

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;
    }
}

然后在您的 activity 中将位图设置到您的模型中 class。喜欢

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

每当根据片段生命周期调用片段时,首先调用OnCreate方法,所以这里我们可以检查位图是否为空并获取位图并使用它。可能不会检查此解决方案,但我认为它适合您。