从uri文件获取真实路径并将其上传到android

时间:2017-11-05 04:50:48

标签: java android file

我想获得Uri文件的真实路径,该文件将被转换为文件格式以上传到数据库(要上传的数据文件)。如何从Uri获取文件格式的文件数据?

public static String getRealPath(Context context, Uri uri) {
        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    // DocumentProvider
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        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 the remote address
            if (isGooglePhotosUri(uri))
                return uri.getLastPathSegment();
            return getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }
    }
    return null;
}

这是我的OnActivityResult代码

                Uri uri = data.getData();
                // Get the path
                System.out.println("url temp : " + uri); 
                String path = getRealPath(this, uri);
                System.out.println("path url : " + path);
                File finalFile = new File(path);

                System.out.println("final file : " + finalFile);

                if (files.size() == jmlDokumen){
                    files.remove(tagId);
                    files.add(tagId, finalFile);
                }
                else{
                    files.add(finalFile);
                }


                String fileName = path.substring(path.lastIndexOf("/")+1);
                et.setText(fileName);

这是我的错误

路径网址:null
11-05 11:07:25.637 30476-30476 / com.project.roy.ezrec W / System.err:java.lang.NullPointerException:尝试调用虚方法'char [] java.lang.String.toCharArray()'在空对象引用上

谁能帮助我吗?请

1 个答案:

答案 0 :(得分:0)

我找到了解决二级SD卡问题的方法(至少它正在我的Xperia XZ Premium上运行)

v0.58