【问题标题】:How to transfer bitmap image from Activity to Fragment如何将位图图像从 Activity 传输到 Fragment
【发布时间】:2017-02-20 07:38:10
【问题描述】:

我的 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 中没有图像显示。如何解决这个问题?

【问题讨论】:

  • 我认为传递参考是个好主意
  • 正如@TahmidRahman 所说,您可以通过参考:)
  • 您可以通过保存在内部存储中来做到这一点..

标签: android android-fragments bitmap android-imageview android-image


【解决方案1】:

转换成字节数组

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

感谢https://stackoverflow.com/a/33797090/4316327

【讨论】:

    【解决方案2】:

    您可以在包中传递您的图像路径/URI。

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

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

    【讨论】:

      【解决方案3】:

      传递图像引用是一种好方法,而不是传递整个图像数据。

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

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

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

      【讨论】:

        【解决方案4】:

        请试试这个方法

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

        }

        【讨论】:

          【解决方案5】:

          在这里,您可以使用 Singleton 类的 getter setter 简单地解决此问题, 首先为您的位图存储创建一个 Ben 类。

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

          然后在您的活动中设置位图到您的模型类中。喜欢

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

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

          【讨论】:

            猜你喜欢
            • 2018-12-03
            • 2013-07-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-01-17
            • 1970-01-01
            • 2018-11-20
            • 1970-01-01
            相关资源
            最近更新 更多