IllegalArgumentException: column '_data' does not exist

前端 未结 4 1441
死守一世寂寞
死守一世寂寞 2020-12-13 19:25

In Nougat, this function is not working.

String path = getRealPathFromURI(this, getIntent().getParcelableExtra(Intent.EXTRA_STREAM));


4条回答
  •  别那么骄傲
    2020-12-13 20:03

    As per given answer by CommonsWare, the solution code is :

    public static String getFilePathFromURI(Context context, Uri contentUri) {
        //copy file and send new file path 
        String fileName = getFileName(contentUri);
        if (!TextUtils.isEmpty(fileName)) {
            File copyFile = new File(TEMP_DIR_PATH + File.separator + fileName);
            copy(context, contentUri, copyFile);
            return copyFile.getAbsolutePath();
        }
        return null;
    }
    
    public static String getFileName(Uri uri) {
        if (uri == null) return null;
        String fileName = null;
        String path = uri.getPath();
        int cut = path.lastIndexOf('/');
        if (cut != -1) {
            fileName = path.substring(cut + 1);
        }
        return fileName;
    }
    
    public static void copy(Context context, Uri srcUri, File dstFile) {
        try {
            InputStream inputStream = context.getContentResolver().openInputStream(srcUri);
            if (inputStream == null) return;
            OutputStream outputStream = new FileOutputStream(dstFile);
            IOUtils.copyStream(inputStream, outputStream);
            inputStream.close();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    I hope this helps you.

    The origin of IOUtils.copy is from this site : https://www.developerfeed.com/copy-bytes-inputstream-outputstream-android/ (May need to change a bit the exception but it works as needed)

提交回复
热议问题