简体   繁体   English

Android-从URI获取文件路径

[英]Android - Getting file path from URI

I have an image processing app. 我有一个图像处理应用程序。 In my code, I pass my service an ArrayList and the service does the rest. 在我的代码中,我将服务传递给ArrayList,其余的由服务完成。 Now I want to extend the functionality of my app and let users be able to go to the gallery, pick one picture and, using the share button, send it to my app to be processed. 现在,我想扩展我的应用程序的功能,并让用户能够进入图库,选择一张图片,然后使用“共享”按钮将其发送到我的应用程序进行处理。 I want to reuse the most code as possible, so I decided that a good way to go would be converting those URIs returned by the send action to actual file paths. 我想尽可能多地重用代码,所以我决定一种不错的方法是将send操作返回的URI转换为实际文件路径。 My solution works as expected with QuickPic, but not with Google Photos. 我的解决方案可以与QuickPic一起使用,但不能与Google相册一起使用。 My code is as follows: 我的代码如下:

//MainFragment.onCreateView()
Intent intent = getActivity().getIntent();
if(Intent.ACTION_SEND.equals(intent.getAction()) {
    handleSingleImage(intent);
}

private void handleSingleImage(Intent intent) {
    Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);

    Log.d("MYURISTRING", uri.toString());
    ArrayList<String> selectedPaths = new ArrayList<String>();

    String path = Utils.getRealPathFromURI(getActivity(), uri);

    selectedPaths.add(path);

    Utils.startProcessPhotosService(getActivity(), MainFragment.this, selectedPaths);
}

//Utils
public static String getRealPathFromURI(Context context, Uri contentUri) {
    /*String[] proj = {MediaStore.Images.Media.DATA};

    CursorLoader cursorLoader = new CursorLoader(context, contentUri, proj, null, null, null);
    Cursor cursor = cursorLoader.loadInBackground();

    int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();

    return cursor.getString(columnIndex);*/

    ContentResolver contentResolver = context.getContentResolver();
    Cursor cursor = contentResolver.query(contentUri, null, null, null, null);
    cursor.moveToFirst();

    String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));

    cursor.close();

    return path;
}

If I test this with a photo from QuickPic app, everything works as expected, and the URI on the log is as follows: 如果我使用来自QuickPic应用程序的照片进行测试,那么一切都会按预期进行,并且日志中的URI如下:

content://media/external/images/media/135695

But if I test this with Google Photos, my app crashes, and the URI is as follows: 但是,如果我使用Google相册进行测试,则我的应用会崩溃,并且URI如下:

content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F135669/ACTUAL

How can I do to support both styles of URI (and, possibly, more than these)? 我该如何支持这两种URI样式(并且可能还支持更多样式)? Thank you 谢谢

so I decided that a good way to go would be converting those URIs returned by the send action to actual file paths 所以我认为一个不错的方法是将send操作返回的URI转换为实际文件路径

That is so not a good way to go. 之所以如此,是不是一个很好的路要走。

As I have already pointed out a couple of times today, and dozens upon dozens of times in the past months, a Uri is not a file . 正如我今天已经指出几次,在过去几个月中数十次提到, Uri并不是文件 You cannot reliably get a local file path for a Uri . 您不能可靠地获取Uri的本地文件路径。 There may not even be a local path, let alone one that you can access. 有可能甚至本地路径,更何况一个可以访问。

If you wish to use the content represented by the Uri , use getContentResolver().openInputStream() , or things that in turn use it (eg, Picasso for image loading). 如果您希望使用Uri表示的内容,请使用getContentResolver().openInputStream()或依次使用它的东西(例如,用于图像加载的Picasso)。

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

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