[英]Is it possible to pass image as argument through intent to activity?
是否可以將Image
作為額外的傳遞給活動?
對於字符串,它是這樣工作的:
intent = new Intent(this, 2Activity.class);
intent.putExtra("String", "xy");
startActivity(intent);
那么有可能對圖像執行此操作嗎? 還是圖像存儲是這樣做的問題?
通過意圖傳遞的數據大小受到限制,因此可能無法正常工作。 通常的方法是傳遞圖像的文件名或resourceID。
好問題。 首先,沒有Image
類,所以我不確定這是否包含在您的庫中。 你是說Bitmap
嗎?
答案通常是不,您不能。 該代碼序列是一個進程間調用,它通過Android的綁定程序內核驅動程序傳遞參數。 參數的大小有一個1MiB的硬限制。 如果使圖像較小,則有可能,但是請考慮將Bitmap
表示為圖像的未壓縮表示形式,因此即使您認為合理大小的圖像也將在內存中占很大的比重。 還要考慮到1MiB緩沖區是為所有IPC與您的進程共享的。 如果您的應用程序的其他組件正在執行IPC,則會減少可用緩沖區。
考慮傳遞可用於讀取圖像數據的ParcelFileDescriptor
,指向可提供圖像數據的內容提供者的content://
URI(請參閱openFile()
)或指向圖像數據的file://
URI。在文件系統上。
正如其他人在這里解釋的那樣,內存將是一個問題。 要解決此問題,可以將圖像存儲在哈希圖中,並在片段和活動之間傳遞密鑰。
您可以使用此包裝器類將圖像保存在內存中。
public class DataWrapper {
private static final DataWrapper instance = new DataWrapper();
private final SparseArray<Object> map = new SparseArray<Object>();
private int currentKeyValue = 1;
public static DataWrapper getInstance() {
return instance;
}
public int putData(final Object data) {
++currentKeyValue;
map.put(currentKeyValue, data);
return currentKeyValue;
}
public Object getData(final int key) {
final Object ret = map.get(key);
map.delete(key);
return ret;
}
}
您可以將Data放入包裝器中,如下所示:
final byte[] imageData;
final int imageDataKey = DataWrapper.getInstance().putData(imageData);
data.putExtra(EXTRA_IMAGE, imageDataKey);
您可以像這樣從包裝器獲取數據:
final int imageDataKey = data.getIntExtra(CameraFragment.EXTRA_IMAGE, 0);
final byte[] data = (byte[]) DataWrapper.getInstance().getData(imageDataKey));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.