如何在两个Activity之间传递Bitmap对象

168

在我的活动中,我创建了一个Bitmap对象,然后我需要启动另一个Activity, 如何从子活动(即将启动的活动)传递这个Bitmap对象?

10个回答

323

Bitmap 实现了 Parcelable 接口,因此您可以通过 intent 传递它:

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);

并在另一端检索它:

Intent intent = getIntent(); 
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

94
如果位图存在于文件或资源中,最好传递位图的“URI”或“ResourceID”,而不是传递整个位图本身。传递整个位图需要大量内存。传递URL所需的内存非常小,并且允许每个活动根据需要加载和缩放位图。 - slayton
4
这个对我没用,但这个可以:https://dev59.com/C2gu5IYBdhLWcg3w3q17 - Houssem
1
@slayton 我们如何将图像作为URI / ResourceIDs传递? 例如?谢谢! - vvavepacket
1
可以传递的位图的最大尺寸是多少? - AtifSayings
1
抛出 android.os.TransactionTooLargeException 异常。 - EAS
显示剩余2条评论

25

23

在活动之间将位图作为包含在Bundle中的可序列化对象传递不是一个好主意,因为可序列化对象(1mb)存在大小限制。您可以将位图存储在内部存储中的文件中,并在多个活动中检索已存储的位图。以下是一些示例代码。

要将位图存储在内部存储中的myImage文件中:

public String createImageFromBitmap(Bitmap bitmap) {
    String fileName = "myImage";//no .png or .jpg needed
    try {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);
        fo.write(bytes.toByteArray());
        // remember close file output
        fo.close();
    } catch (Exception e) {
        e.printStackTrace();
        fileName = null;
    }
    return fileName;
}

接下来的活动中,您可以使用以下代码将此文件myImage解码为位图:

//here context can be anything like getActivity() for fragment, this or MainActivity.this
Bitmap bitmap = BitmapFactory.decodeStream(context.openFileInput("myImage"));

注意:省略了许多检查空值和缩放位图的步骤。


2
这段代码无法编译 - 无法解析方法 openFileOutput - Hawklike

5

压缩和发送Bitmap

Bitmap太大时,接受的答案将崩溃。我相信它有1MB的限制。必须将Bitmap压缩为不同的文件格式,如一个由ByteArray表示的JPG,然后可以通过Intent安全地传递。

实现

该函数包含在一个单独的线程中,使用Kotlin协程,因为Bitmap压缩在从url String创建Bitmap之后进行链接。Bitmap的创建需要一个单独的线程,以避免应用程序未响应(ANR)错误。

所用概念

  • Kotlin协程 笔记
  • 下面使用了加载、内容、错误(LCE)模式。如果感兴趣,您可以在此演讲和视频中了解更多信息。
  • 使用LiveData返回数据。我收集了我最喜欢的LiveData资源,在这些笔记中。
  • 步骤3中,toBitmap()是一个Kotlin扩展函数,需要将该库添加到应用程序依赖项中。

代码

1. 在创建Bitmap后将Bitmap压缩为JPGByteArray

Repository.kt

suspend fun bitmapToByteArray(url: String) = withContext(Dispatchers.IO) {
    MutableLiveData<Lce<ContentResult.ContentBitmap>>().apply {
        postValue(Lce.Loading())
        postValue(Lce.Content(ContentResult.ContentBitmap(
            ByteArrayOutputStream().apply {
                try {                     
                    BitmapFactory.decodeStream(URL(url).openConnection().apply {
                        doInput = true
                        connect()
                    }.getInputStream())
                } catch (e: IOException) {
                   postValue(Lce.Error(ContentResult.ContentBitmap(ByteArray(0), "bitmapToByteArray error or null - ${e.localizedMessage}")))
                   null
                }?.compress(CompressFormat.JPEG, BITMAP_COMPRESSION_QUALITY, this)
           }.toByteArray(), "")))
        }
    }

ViewModel.kt

//Calls bitmapToByteArray from the Repository
private fun bitmapToByteArray(url: String) = liveData {
    emitSource(switchMap(repository.bitmapToByteArray(url)) { lce ->
        when (lce) {
            is Lce.Loading -> liveData {}
            is Lce.Content -> liveData {
                emit(Event(ContentResult.ContentBitmap(lce.packet.image, lce.packet.errorMessage)))
            }
            is Lce.Error -> liveData {
                Crashlytics.log(Log.WARN, LOG_TAG,
                        "bitmapToByteArray error or null - ${lce.packet.errorMessage}")
            }
        }
    })
}

2. 通过 Intent 将图像以 ByteArray 形式传递。

在此示例中,从一个 Fragment 传递到一个 Service。如果在两个 Activities 之间共享,则是相同的概念。

Fragment.kt

ContextCompat.startForegroundService(
    context!!,
    Intent(context, AudioService::class.java).apply {
        action = CONTENT_SELECTED_ACTION
        putExtra(CONTENT_SELECTED_BITMAP_KEY, contentPlayer.image)
    })

3. 将 ByteArray 转换回 Bitmap

Utils.kt

fun ByteArray.byteArrayToBitmap(context: Context) =
    run {
        BitmapFactory.decodeByteArray(this, BITMAP_OFFSET, size).run {
            if (this != null) this
            // In case the Bitmap loaded was empty or there is an error I have a default Bitmap to return.
            else AppCompatResources.getDrawable(context, ic_coinverse_48dp)?.toBitmap()
        }
    }

5

如果图片过大并且无法保存和加载到存储器中,您应该考虑仅在接收活动(receiving activity)中使用全局静态引用来引用位图,该引用将在onDestory上被重置为null,只有当“isChangingConfigurations”返回true时。


3

因为Intent有大小限制,我使用公共静态对象将位图从服务传递到广播....

public class ImageBox {
    public static Queue<Bitmap> mQ = new LinkedBlockingQueue<Bitmap>(); 
}

传递我的服务

private void downloadFile(final String url){
        mExecutorService.submit(new Runnable() {
            @Override
            public void run() {
                Bitmap b = BitmapFromURL.getBitmapFromURL(url);
                synchronized (this){
                    TaskCount--;
                }
                Intent i = new Intent(ACTION_ON_GET_IMAGE);
                ImageBox.mQ.offer(b);
                sendBroadcast(i);
                if(TaskCount<=0)stopSelf();
            }
        });
    }

我的广播接收器
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            LOG.d(TAG, "BroadcastReceiver get broadcast");

            String action = intent.getAction();
            if (DownLoadImageService.ACTION_ON_GET_IMAGE.equals(action)) {
                Bitmap b = ImageBox.mQ.poll();
                if(b==null)return;
                if(mListener!=null)mListener.OnGetImage(b);
            }
        }
    };

1
可能有点晚,但是我可以帮忙。在第一个片段或活动中声明一个类...例如。
   @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        description des = new description();

        if (requestCode == PICK_IMAGE_REQUEST && data != null && data.getData() != null) {
            filePath = data.getData();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
                imageView.setImageBitmap(bitmap);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                constan.photoMap = bitmap;
            } catch (IOException e) {
                e.printStackTrace();
            }
       }
    }

public static class constan {
    public static Bitmap photoMap = null;
    public static String namePass = null;
}

在第二个类/片段中执行以下操作...
Bitmap bm = postFragment.constan.photoMap;
final String itemName = postFragment.constan.namePass;

希望它有所帮助。

1
所有上述解决方案对我都不起作用,将位图作为parceableByteArray发送也会生成错误android.os.TransactionTooLargeException:data parcel size解决方案
  1. 将位图保存在内部存储中,如下所示:
public String saveBitmap(Bitmap bitmap) {
        String fileName = "ImageName";//no .png or .jpg needed
        try {
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);
            fo.write(bytes.toByteArray());
            // remember close file output
            fo.close();
        } catch (Exception e) {
            e.printStackTrace();
            fileName = null;
        }
        return fileName;
    }

    中,将putExtra(String)作为参数发送。
    Intent intent = new Intent(ActivitySketcher.this,ActivityEditor.class);
    intent.putExtra("KEY", saveBitmap(bmp));
    startActivity(intent);
    

    在其他活动中接收它的代码如下:
    if(getIntent() != null){
      try {
               src = BitmapFactory.decodeStream(openFileInput("myImage"));
           } catch (FileNotFoundException e) {
                e.printStackTrace();
          }
    
     }
    
    
    

0
你可以创建一个位图传输。尝试这个...
在第一个类中:
1)创建:
private static Bitmap bitmap_transfer;

2) 创建 getter 和 setter

public static Bitmap getBitmap_transfer() {
    return bitmap_transfer;
}

public static void setBitmap_transfer(Bitmap bitmap_transfer_param) {
    bitmap_transfer = bitmap_transfer_param;
}

3) 设置图片:

ImageView image = (ImageView) view.findViewById(R.id.image);
image.buildDrawingCache();
setBitmap_transfer(image.getDrawingCache());

然后,在第二个类中:

ImageView image2 = (ImageView) view.findViewById(R.id.img2);
imagem2.setImageDrawable(new BitmapDrawable(getResources(), classe1.getBitmap_transfer()));

-2
在我的情况下,上述提到的方法对我没有起作用。每次我将位图放入意图中时,第二个活动都不会启动。当我将位图作为byte[]传递时也是如此。
我遵循了这个link,它像魅力一样快速地工作了。
package your.packagename

import android.graphics.Bitmap;

public class CommonResources { 
      public static Bitmap photoFinishBitmap = null;
}

在我的第一个活动中:

Constants.photoFinishBitmap = photoFinishBitmap;
Intent intent = new Intent(mContext, ImageViewerActivity.class);
startActivity(intent);

这是我第二个Activity的onCreate():

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bitmap photo = Constants.photoFinishBitmap;
    if (photo != null) {
        mViewHolder.imageViewerImage.setImageDrawable(new BitmapDrawable(getResources(), photo));
    }
}

我尝试了这个,但不起作用。我按照链接所示,看起来你应该使用CommonResources.photoFinishBitmap而不是Constants.photoFinishBitmap - Nathan Hutton
1
不良实践。在整个进程重新创建时(例如,由于运行时更改应用程序的权限),Activity类中的静态字段会发生什么情况?答案是NPE。 - Alex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接