簡體   English   中英

無法從android照片應用獲取圖像

[英]Not getting the image from android photos app

我在這里這里都看到過類似的問題。

但是這些仍然不能解決我的問題,所以我要發布一個新問題。

這是我的代碼,用於獲取在“ 照片”應用中選擇的圖像的路徑。 但是我沒有得到cursor.getColumnIndexOrThrow(column); 值為0 ,cursor.getString(column_index)值為null

Cursor cursor = null;
        final String column = "_data";
        final String[] projection = {
                column
        };

        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(column);
                return cursor.getString(column_index);
            }
        } finally {
            if (cursor != null)
                cursor.close();
        }

誰能幫我解決這個問題。 如果我的問題太籠統,請告訴我。 我會更新我的問題。

更新:開始意圖:

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, getString(R.string.selectPhoto)), RESULT_LOAD_IMAGE);

在onActivityResult中:

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {

    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = getContentResolver().query(contentURI, projection, null, null, null);
    if (cursor == null) {
          return contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(projection[0]);
        return cursor.getString(idx);
    }
}

問候

用戶下面的代碼

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Common.Action_Gallery:
                    if (data != null) {
                        Uri selectedImage = data.getData();
                        String[] filePathColumn = {MediaStore.Images.Media.DATA};
                        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                        cursor.moveToFirst();
                        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                        String picturePath = cursor.getString(columnIndex);
                        cursor.close();                           
                    }
                    break;   
            }
        }
}

暫無
暫無

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

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