android - 文件路径总是在 Lollipop android中返回null

标签 android imageview android-5.0-lollipop filepath internal-storage

这是我从内部存储(画廊)获取图像时的代码。 在 Lollipop 文件路径中返回始终为空。

if (requestCode == PICK_IMAGE)  {
        if(resultCode == RESULT_OK){
            //image successfully picked
            // launching upload activity
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
            cursor.moveToFirst();
            columnindex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
            file_path = cursor.getString(columnindex);
            Log.d(getClass().getName(), "file_path"+file_path);
            fileUri = Uri.parse("file://" + file_path);
            cursor.close();
            launchUploadActivity(true, PICK_IMAGE);
        }else if (resultCode == RESULT_CANCELED) {
            // user cancelled recording
            Toast.makeText(getApplicationContext(),"User cancelled image  selection", Toast.LENGTH_SHORT).show();
        } else {
            // failed to record video
            Toast.makeText(getApplicationContext(),"Sorry! failed to pick image", Toast.LENGTH_SHORT).show();
        }

最佳答案

谢谢大家,我找到了解决方案。

    Uri selectedImage = data.getData();
            String wholeID = DocumentsContract.getDocumentId(selectedImage);

            // Split at colon, use second item in the array
            String id = wholeID.split(":")[1];

            String[] column = { MediaStore.Images.Media.DATA };     

            // where id is equal to             
            String sel = MediaStore.Images.Media._ID + "=?";

            Cursor cursor = getContentResolver().
                                      query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
                                      column, sel, new String[]{ id }, null);

            String filePath = "";

            int columnIndex = cursor.getColumnIndex(column[0]);

            if (cursor.moveToFirst()) {
                filePath = cursor.getString(columnIndex);
            }   
            cursor.close();
            setImageFromIntent(filePath);

关于android - 文件路径总是在 Lollipop android中返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29144367/

相关文章:

java - 定义 onClick 参数不在 xml 文件 android

android - 使用字符串访问 Android 可绘制资源

Android:黑屏加载图像

android - 在android中使用Glide从图像中获取位图

android - Material Design 排版 - 标题、标题、间距、文本外观

bluetooth-lowenergy - Android : iBeacon - read its advertisement (e. g txPower)

java - Android 无法执行 rm -r

java - 算法 HmacPBESHA256 不可用(Flutter 构建失败)

android - 取消选中 RadioButtonGroup 中的所有 RadioButton

android - 通知图标,在某些设备上为全白色,在其他设备上为多色。为什么?