簡體   English   中英

如何在Kitkat Android中將圖像從一個活動發送到另一個活動?

[英]How to send image from one activity to another in kitkat android?

在第一個活動中:

Intent i = new Intent(FirstActivity.this, SecondActivity.class);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bytes = stream.toByteArray();
i.putExtra("image", bytes);
startActivity(i);

在第二個活動中:

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

if (bmp != null) {         
    iv_1.setImageBitmap(bmp);
}

這適用於所有設備和版本。 但這對Kitkat不起作用,為什么呢? 如何解決奇巧的問題?

通過意圖傳遞如此巨大的文件不是一個好習慣。 這將減慢啟動新活動的過程。

嘗試對圖像進行靜態引用,並在下一個活動中使用它。 完成后,立即將其設置為null

使用Map<String, Bitmap>一個單例類,該類將保留您需要的所有圖像,並通過意圖僅發送其鍵名。

將位圖從正在進行的活動傳遞到另一個活動對性能不利。

只需嘗試將位圖保存在內存中,然后將位圖的“路徑”發送到另一個活動,然后使用BitmapFactory.decodeFile(pathName); 另一個活動中的方法從路徑獲取位圖。

在您的第一個活動中,將imageview轉換為位圖

imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();

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

在第二活動中

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

它也對奇巧工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM