繁体   English   中英

如何从URI android Lollipop获取真实路径

[英]How to get Real path from URI android Lollipop

我已经尝试过了,但是没有运气。

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();

        nameView.setText(selectedImage.toString());
        realPath = getRealPathFromURI(this, selectedImage);
        Toast.makeText(MainActivity.this, realPath, Toast.LENGTH_LONG).show();

    }
}

public 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();
        }
    }
}

在TOAST中显示时,我得到的是null。 我正在运行棒棒糖。 请有人详细说明如何执行此操作。 我能够毫无问题地获取URI,但我唯一的问题是无法将其转换为存储在数据库中的真实路径。

谢谢

此问题主要发生在Nexus等Google本机设备上,因为在这些设备中,唯一的图片库是Google照片。 在Google照片中,您不仅可以选择设备上存在的那些图像,还可以选择存储在云存储中的那些照片。 尝试下面提到的更新代码,让我知道它是否有效。 您的意图应具有Intent的作用。ACTION_GET_CONTENT

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == ATTACHMENT_PHOTO_GALLERY && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        nameView.setText(selectedImage.toString());
        String realPath = getRealPathFromURI(this, selectedImage);
        Toast.makeText(MainActivity.this, realPath, Toast.LENGTH_LONG).show();

    }
}

public String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    String path = 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();
        path = cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    //The above code will fail to get file path if the file is selected from google photos.
    //Then this will be the alternative approach
    if (path == null && contentUri.toString().startsWith("content://com.google.android.apps.photos.content")) {
        Bitmap requiredImage = null;
        //Below key should be used for saving the newly downloaded image path as 
        //photoIdentificationKey as Key parameter and newly generated path as Value.
        //An additional check should be implemented on your storage that whether any local
        //path exists for corresponding photoIdentificationKey, if yes then use the existing
        //local path else download new image.
        String photoIdentificationKey = Uri.parse(contentUri.getLastPathSegment()).getLastPathSegment();
        BitmapFactory.Options options = new BitmapFactory.Options();
        InputStream inputStream;

        try {
            inputStream = getApplication().getContentResolver().openInputStream(contentUri);
            requiredImage = BitmapFactory.decodeStream(inputStream, null, options);

            //Save the newly downloaded image to your isolated storage and return the path
            //on which this new image has been saved.

            inputStream.close();
            path = saveAndReturnPathForSavedImage(requiredImage);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    return path;
}

private String saveAndReturnPathForSavedImage(Bitmap bmp) {
    String path = Environment.getExternalStorageDirectory().toString();
    OutputStream out = null;
    File file = new File(path, "IMAGE"+ Calendar.getInstance().getTime()+".jpg"); // the File to save to
    try {
        out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return file.getPath();
}

如果您的Intent data不为null,则可以尝试此操作。 我已经使用此代码从图库中获取图像路径,并且该图像在所有设备中均有效。

  Uri selectedImage = data.getData();
                        String[] filePath = {MediaStore.Images.Media.DATA};
                        Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
                        c.moveToFirst();
                        int columnIndex = c.getColumnIndex(filePath[0]);
                        String picturePath = c.getString(columnIndex);

                        c.close();
                        Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));

Log.e("path of image from gallery......******************.........", picturePath + "");

注意:请确保您的data不应为null,否则它将提供NullPointerException

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM