1 回答

TA貢獻1845條經驗 獲得超8個贊
為了將圖像文件傳遞給 Android 廣播接收器,您必須將文件轉換為字節數組并使用putExtra方法發送
intent.putExtra("myImage", convertBitmapToByteArray(bitmapImage));
byte[] convertBitmapToByteArray(Bitmap bitmap) {
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
Log.e(BitmapUtils.class.getSimpleName(), "ByteArrayOutputStream was not closed");
}
}
}
}
然后您可以在廣播接收器中轉換回圖像
byte[] byteArray = intent.getByteArrayExtra("myImage");
Bitmap myImage = convertCompressedByteArrayToBitmap(byteArray);
Bitmap convertCompressedByteArrayToBitmap(byte[] src) {
return BitmapFactory.decodeByteArray(src, 0, src.length);
}
添加回答
舉報