温馨提示:本文翻译自stackoverflow.com,查看原文请点击:libgdx - Using android gallery so user can choose their photo for avatar, having problems with the path of th

libgdx - 使用android画廊,以便用户可以选择自己的照片作为化身,而th的路径有问题

发布于 2020-03-27 10:27:19

我不会打印出整个堆栈跟踪记录,因为它很大,但是从本质上来说,我正在尝试为文件'document / image:136635'创建一个FileHandle,但没有得到这样的文件或目录。

com.badlogic.gdx.utils.GdxRuntimeException:无法加载文件:/ document / image:136635

引起原因:android.system.ErrnoException:打开失败:ENOENT(无此类文件或目录)

So I'm using the path 'document/image:136635' because I launched an activity for the android gallery app so the user can select a photo of themselves for the avatar. The selection seems to works ok, we can select an image and my program continues afterwards, but they when I try to create a filehandle for the image so I can actually draw it in my game I have problems. When I analyse the data returned from the activity the path of the photo they selected comes back as 'document/image:136635'. Is that a valid path for an image ordinarily? If I use a file viewer on my phone I can see that there is a document folder but nothing is in it.

Any advice appreciated. Thanks.

edit: So the info in the URI is (in order):

image:136635

image/jpeg

IMG_20190702_175942.jpg

1562065182000

5

1285506

因此,我可以获取实际的文件名,但仍然无法获取真实的路径?我不认为...

查看更多

查看更多

提问者
user8810083
被浏览
61
Prachi Singh 2019-07-03 18:14
 private static final int GALLERY_PICK = 1;


Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(galleryIntent, GALLERY_PICK);


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

if (requestCode == GALLERY_PICK && resultCode == RESULT_OK && data != null && 
data.getData() != null) {

    Uri uri = data.getData();

    try {

     String filePath = getPathNew(this,uri);  //return actual path

    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

获取实际路径

@SuppressLint("NewApi")
    public static String getPathNew(final Context context, final Uri uri) {

        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

        // DocumentProvider
        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
            // ExternalStorageProvider
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                if ("primary".equalsIgnoreCase(type)) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                }
                // TODO handle non-primary volumes
            }
            // DownloadsProvider
            else if (isDownloadsDocument(uri)) {

                final String id = DocumentsContract.getDocumentId(uri);
                final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                        Long.valueOf(id));
                return getDataColumn(context, contentUri, null, null);
            }
            // MediaProvider
            else if (isMediaDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                Uri contentUri = null;
                if ("image".equals(type)) {
                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }

                final String selection = "_id=?";
                final String[] selectionArgs = new String[]{split[1]};

                return getDataColumn(context, contentUri, selection, selectionArgs);
            }
        }
        // MediaStore (and general)
        else if ("content".equalsIgnoreCase(uri.getScheme())) {
            return getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

        return null;
    }

    public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

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

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is ExternalStorageProvider.
     */
    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is DownloadsProvider.
     */
    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is MediaProvider.
     */
    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }

在清单中添加文件READ WRITE 权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

发布
问题

分享
好友

手机
浏览

扫码手机浏览