簡體   English   中英

意圖從圖庫中獲取圖像

[英]Get Image from Gallery with Intent

我想使用share命令從圖庫中獲取圖像。

我當前的代碼是:

Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    if (Intent.ACTION_SEND.equals(action) && type != null) {
        Log.d("Test","Simple SEND");
        Uri imageUri = (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);
        if (imageUri != null) {
            InputStream iStream = getContentResolver().openInputStream(imageUri);
        }
    } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
        Log.d("Test", "Multiple SEND");
    }

imageUri的值是:content:// media / external / images / media / 37

但是函數“ openInputStream”拋出錯誤“ java.io.FileNotFoundException”。

通過以下功能,我可以獲得圖像的真實路徑。

public static String getRealPathFromUri(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

但我不知道如何將其轉換為位圖。

Uri不是File new File(imageUri.toString())總是錯誤的。 imageUri.getPath()僅在Uri的方案為file時才起作用,並且在您的情況下,該方案可能是content

使用ContentResolveropenInputStream()Uri標識的文件或內容上獲取InputStream 然后,將該流傳遞給BitmapFactory.decodeStream()

另外,請在后台線程上解碼位圖,以免在進行此工作時凍結UI。

暫無
暫無

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

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