Java 下载文件夹中带有文件选择器的Android Studio出错

Java 下载文件夹中带有文件选择器的Android Studio出错,java,android,file,android-studio,explorer,Java,Android,File,Android Studio,Explorer,我的问题 我目前正在用Android Studio开发一个Android应用程序。 要选择一个文件,用户点击一个按钮,浏览器打开,他可以选择一个文件。 对于这个过程,我使用了一个名为FileChooser.Java的助手类。只要用户不从下载文件夹中选择文件,整个系统就可以正常工作 如果用户试图从下载文件夹中选择文件,则会发生以下情况: NumberFormatException:对于输入字符串:msf:80123 文件选择器返回的URL: content://com.android.provid

我的问题

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

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

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

文件选择器返回的URL: content://com.android.providers.downloads.documents/document/msf:80662

这是因为helper类需要一个长类型。删除msf:并将ID添加到末尾也不起作用。看起来Android在下载文件夹中的所有内容中都添加了这个msf:tag

我正在寻找一个有效的解决方案来调用文件从下载文件夹太

电话

FileChooser类中的异常调用

助手类文件选择器

在onActivityResult中:

InputStream dataStream = getContentResolver().openInputStream(data.getData());
扔掉getPath函数

这适用于所有尿酸。

在onActivityResult中:

InputStream dataStream = getContentResolver().openInputStream(data.getData());
扔掉getPath函数


这适用于所有URI。

唯一的问题是获取下载目录的路径或msf和NumberFormat异常,请尝试此项。这对我来说是正确的

package com.example.bookingmelbourne;
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;
import android.provider.OpenableColumns;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtils {
    private static final String TAG = "FileUtils";
@WorkerThread
@Nullable
public static String getReadablePathFromUri(Context context, Uri uri) {

    String path = null;
    if ("file".equalsIgnoreCase(uri.getScheme())) {
        path = uri.getPath();
    }

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
        path = getPath(context, uri);
    }

    if (TextUtils.isEmpty(path)) {
        return path;
    }

    Log.d(TAG, "get path from uri: " + path);
    if (!isReadablePath(path)) {
        int index = path.lastIndexOf("/");
        String name = path.substring(index + 1);
        String dstPath = context.getCacheDir().getAbsolutePath() + File.separator + name;
        if (copyFile(context, uri, dstPath)) {
            path = dstPath;
            Log.d(TAG, "copy file success: " + path);
        } else {
            Log.d(TAG, "copy file fail!");
        } 
    }
    return path;
}

public static String getPath(final Context context, final Uri uri) {
    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            Log.d("External Storage", docId);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }
        } else if (isDownloadsDocument(uri)) {

            String dstPath = context.getCacheDir().getAbsolutePath() + File.separator + getFileName(context,uri);

             if (copyFile(context, uri, dstPath)) {
                Log.d(TAG, "copy file success: " + dstPath);
                return dstPath;

            } else {
                Log.d(TAG, "copy file fail!");
            }


        } 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);
        }
    } else if ("content".equalsIgnoreCase(uri.getScheme())) {
        return getDataColumn(context, uri, null, null);
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }
    return null;
}

public static String getFileName(Context context, Uri uri) {

    Cursor cursor = context.getContentResolver().query(uri,null,null,null,null);
    int nameindex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
    cursor.moveToFirst();

    return  cursor.getString(nameindex);
}


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

private static boolean isExternalStorageDocument(Uri uri) {
    return "com.android.externalstorage.documents".equals(uri.getAuthority());
}

private static boolean isDownloadsDocument(Uri uri) {
    return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}

private static boolean isMediaDocument(Uri uri) {
    return "com.android.providers.media.documents".equals(uri.getAuthority());
}

private static boolean isReadablePath(@Nullable String path) {
    if (TextUtils.isEmpty(path)) {
        return false;
    }
    boolean isLocalPath;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        if (!TextUtils.isEmpty(path)) {
            File localFile = new File(path);
            isLocalPath = localFile.exists() && localFile.canRead();
        } else {
            isLocalPath = false;
        }
    } else {
        isLocalPath = path.startsWith(File.separator);
    }
    return isLocalPath;
}

private static boolean copyFile(Context context, Uri uri, String dstPath) {
    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {
        inputStream = context.getContentResolver().openInputStream(uri);
        outputStream = new FileOutputStream(dstPath);

        byte[] buff = new byte[100 * 1024];
        int len;
        while ((len = inputStream.read(buff)) != -1) {
            outputStream.write(buff, 0, len);
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return true;
}

  }
之后,可以通过传递上下文和uri对象调用此方法

 String realPath = FileUtils.getReadablePathFromUri(context,uri)

唯一的问题是获取下载目录的路径或msf和NumberFormat异常,请尝试此项。这对我来说是正确的

package com.example.bookingmelbourne;
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;
import android.provider.OpenableColumns;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtils {
    private static final String TAG = "FileUtils";
@WorkerThread
@Nullable
public static String getReadablePathFromUri(Context context, Uri uri) {

    String path = null;
    if ("file".equalsIgnoreCase(uri.getScheme())) {
        path = uri.getPath();
    }

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
        path = getPath(context, uri);
    }

    if (TextUtils.isEmpty(path)) {
        return path;
    }

    Log.d(TAG, "get path from uri: " + path);
    if (!isReadablePath(path)) {
        int index = path.lastIndexOf("/");
        String name = path.substring(index + 1);
        String dstPath = context.getCacheDir().getAbsolutePath() + File.separator + name;
        if (copyFile(context, uri, dstPath)) {
            path = dstPath;
            Log.d(TAG, "copy file success: " + path);
        } else {
            Log.d(TAG, "copy file fail!");
        } 
    }
    return path;
}

public static String getPath(final Context context, final Uri uri) {
    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            Log.d("External Storage", docId);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }
        } else if (isDownloadsDocument(uri)) {

            String dstPath = context.getCacheDir().getAbsolutePath() + File.separator + getFileName(context,uri);

             if (copyFile(context, uri, dstPath)) {
                Log.d(TAG, "copy file success: " + dstPath);
                return dstPath;

            } else {
                Log.d(TAG, "copy file fail!");
            }


        } 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);
        }
    } else if ("content".equalsIgnoreCase(uri.getScheme())) {
        return getDataColumn(context, uri, null, null);
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }
    return null;
}

public static String getFileName(Context context, Uri uri) {

    Cursor cursor = context.getContentResolver().query(uri,null,null,null,null);
    int nameindex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
    cursor.moveToFirst();

    return  cursor.getString(nameindex);
}


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

private static boolean isExternalStorageDocument(Uri uri) {
    return "com.android.externalstorage.documents".equals(uri.getAuthority());
}

private static boolean isDownloadsDocument(Uri uri) {
    return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}

private static boolean isMediaDocument(Uri uri) {
    return "com.android.providers.media.documents".equals(uri.getAuthority());
}

private static boolean isReadablePath(@Nullable String path) {
    if (TextUtils.isEmpty(path)) {
        return false;
    }
    boolean isLocalPath;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        if (!TextUtils.isEmpty(path)) {
            File localFile = new File(path);
            isLocalPath = localFile.exists() && localFile.canRead();
        } else {
            isLocalPath = false;
        }
    } else {
        isLocalPath = path.startsWith(File.separator);
    }
    return isLocalPath;
}

private static boolean copyFile(Context context, Uri uri, String dstPath) {
    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {
        inputStream = context.getContentResolver().openInputStream(uri);
        outputStream = new FileOutputStream(dstPath);

        byte[] buff = new byte[100 * 1024];
        int len;
        while ((len = inputStream.read(buff)) != -1) {
            outputStream.write(buff, 0, len);
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return true;
}

  }
之后,可以通过传递上下文和uri对象调用此方法

 String realPath = FileUtils.getReadablePathFromUri(context,uri)

非常感谢你。这个解决方案是有效的,但是有些东西改变了。我正在制作一个PGP工具,这个InputStream包含一个从所选文件读取的公钥。如果我按照您的方式创建InputStream,PGP库将不再将其识别为公钥。但是如果我把它打印出来,它看起来是一样的,你知道为什么会这样吗?对不起,没有一个想法。非常感谢。这个解决方案是有效的,但是有些东西改变了。我正在制作一个PGP工具,这个InputStream包含一个从所选文件读取的公钥。如果我按照您的方式创建InputStream,PGP库将不再将其识别为公钥。但是如果我把它打印出来,它看起来是一样的,你知道为什么会发生这种情况吗?对不起,没有一个想法。