【问题标题】:Select image from gallery for some device it null从图库中为某些设备选择图像,它为空
【发布时间】:2016-08-02 16:30:55
【问题描述】:

我正在从下面的代码库中获取图像

 Uri filePath = data.getData();
 String[] filePathColumn = {MediaStore.Images.Media.DATA,
                    MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.MIME_TYPE};
            Cursor cursor =
                    mCon.getContentResolver().query(filePath, filePathColumn, null, null, null);

但是当我从某些设备中的文件探索中选择图像时,光标始终为空,但如果我从图库应用程序中选择相同的图像,它可以工作......为什么会这样?

【问题讨论】:

    标签: android android-cursor


    【解决方案1】:

    试试这个代码

    private String uriToFilename(Uri uri) {
            String path = null;
    
            if (Build.VERSION.SDK_INT < 11) {
                path = RealPathUtil.getRealPathFromURI_BelowAPI11(this, uri);
            } else if (Build.VERSION.SDK_INT < 19) {
                path = RealPathUtil.getRealPathFromURI_API11to18(this, uri);
            } else {
                path = RealPathUtil.getRealPathFromURI_API19(this, uri);
            }
    
            return path;
        }
    

    创建 RealOathUtil.java

    public class RealPathUtil {
        @SuppressLint("NewApi")
        public static String getRealPathFromURI_API19(Context context, Uri uri) {
            Log.e("uri", uri.getPath());
            String filePath = "";
            if (DocumentsContract.isDocumentUri(context, uri)) {
                String wholeID = DocumentsContract.getDocumentId(uri);
                Log.e("wholeID", wholeID);
    // Split at colon, use second item in the array
                String[] splits = wholeID.split(":");
                if (splits.length == 2) {
                    String id = splits[1];
    
                    String[] column = {MediaStore.Images.Media.DATA};
    // where id is equal to
                    String sel = MediaStore.Images.Media._ID + "=?";
                    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                            column, sel, new String[]{id}, null);
                    int columnIndex = cursor.getColumnIndex(column[0]);
                    if (cursor.moveToFirst()) {
                        filePath = cursor.getString(columnIndex);
                    }
                    cursor.close();
                }
            } else {
                filePath = uri.getPath();
            }
            return filePath;
        }
    
        @SuppressLint("NewApi")
        public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
            String[] proj = {MediaStore.Images.Media.DATA};
            String result = null;
            CursorLoader cursorLoader = new CursorLoader(
                    context,
                    contentUri, proj, null, null, null);
            Cursor cursor = cursorLoader.loadInBackground();
            if (cursor != null) {
                int column_index =
                        cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                result = cursor.getString(column_index);
            }
            return result;
        }
    
        public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) {
            String[] proj = {MediaStore.Images.Media.DATA};
            Cursor 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);
        }
    }
    

    现在在 onactivtyResult() 中使用这个

    Uri selectedImageUri = data.getData();
     String selectedImagePath = uriToFilename(selectedImageUri);
    

    【讨论】:

    • 在此我如何获取 url
    • 在 getRealPathFromURI_API19 中,我总是可以从所有方法中获取光标,因为我想要文件路径,然后是文件类型
    • 可以告诉我如何在这个方法中获得 pdf mime
    • 对不起。你想要什么我没明白。请解释一下
    • 所以从你的方法我得到的路径现在我想要文件的路径,文件的 mime
    【解决方案2】:

    你应该试试这个。

    Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    

    【讨论】:

    • 我正在使用 Intent intent = new Intent();意图.setType("/");意图.setAction(意图.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "选择图片"), PICK_IMAGE_REQUEST);
    【解决方案3】:

    试试这个代码。

    打开画廊:

    Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_PICK);
        startActivityForResult(Intent.createChooser(intent, "Complete action using"), 1);
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1 && resultCode == RESULT_OK) {
            Uri selectedImageUri = data.getData();
            mImagePath = getPath(selectedImageUri);
            mBitmap = BitmapFactory.decodeFile(mImagePath);
            mProfilePic.setImageBitmap(mBitmap);
        }
    }
    

    获取图片的实际路径:

    public String getPath(Uri uri) {
        String[] projection = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    

    希望对您有所帮助。

    【讨论】:

    • nope 在此代码中,如果从文件浏览器中选择图像,则光标为空
    • 这是相同的代码,你可以检查 qus
    • 在我的代码中,它给出了方法调用 cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA) 可能产生 NullPointerException 的消息。并不总是产生 NullPointerException。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 2016-02-18
    相关资源
    最近更新 更多