下载文件夹中带有文件选择器的 Android Studio 中的错误 [英] Error in Android Studio with File Chooser in Download Folder

查看:38
本文介绍了下载文件夹中带有文件选择器的 Android Studio 中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题

我目前正在使用 Android Studio 开发 Android 应用程序.要选择一个文件,用户点击一个按钮,浏览器打开,他可以选择一个文件.对于这个过程,我使用了一个名为 FileChooser.Java 的辅助类.只要用户不从下载文件夹中选择文件,整个系统就会运行得很好.

I am currently developing an Android application with Android Studio. To choose a file the user clicks on a button, the explorer opens and he can select a file. For this process I used a helper class called FileChooser.Java. This whole system functions very well as long as the user doesn't select a file from the Download folder.

如果用户尝试从下载文件夹中选择文件,则会发生这种情况:

If the users tries to select a file from the Download folder this happens:

NumberFormatException:对于输入字符串:msf:80123"

FileChooser 返回的 URL:content://com.android.providers.downloads.documents/document/msf:80662

The URL the FileChooser returns: content://com.android.providers.downloads.documents/document/msf:80662

这是因为辅助类需要一个 long 类型.删除 msf: 并将 ID 添加到末尾也不起作用.似乎 Android 将此 msf: 标记添加到下载文件夹中的所有内容.

This is because the helper class expects a long type. Removing the msf: and adding the ID to the end doesn't work either. It seems like Android adds this msf: tag to everything in the Download folder.

我也在寻找一种有效的解决方案来调用下载文件夹中的文件.

I am looking for a valid solution to call files from the Download folder too.

电话

Uri selectedFile = data.getData();
InputStream dataStream = new FileInputStream(FileChooser.getPath(getContext(), selectedFile));

FileChooser 类中的异常调用

 final Uri contentUri = ContentUris.withAppendedId(
       Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

Helper 类 FileChooser

package com.example.dsvconverter.helper;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;


public class FileChooser {

    /**
     * Get a file path from a Uri. This will get the the path for Storage Access
     * Framework Documents, as well as the _data field for the MediaStore and
     * other file-based ContentProviders.
     *
     * @param context The context.
     * @param uri The Uri to query.
     * @author paulburke
     */
    public static String getPath(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 String[] split = id.split(":");
                final String type = split[0];

                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;
    }

    /**
     * Get the value of the data column for this Uri. This is useful for
     * MediaStore Uris, and other file-based ContentProviders.
     *
     * @param context The context.
     * @param uri The Uri to query.
     * @param selection (Optional) Filter used in the query.
     * @param selectionArgs (Optional) Selection arguments used in the query.
     * @return The value of the _data column, which is typically a file path.
     */
    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());
    }
}

推荐答案

In onActivityResult() :

In onActivityResult() :

InputStream dataStream = getContentResolver().openInputStream(data.getData());

扔掉那个 getPath() 函数.

Throw away that getPath() function.

这适用于所有 uri.

This works for all uries.

这篇关于下载文件夹中带有文件选择器的 Android Studio 中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆