简体   繁体   中英

Create file from Uri exception occurs

While creating a file from Uri and exception Occured. The value of exception shows null.

try {
File mFile = new File(new URI(mediaUri.toString()));
data = readFile(mFile);     
 } catch (Exception e) {
Log.e("Error", e.getLocalizedMessage());
}   

Please help

Thanks in advance.

The issue was while creating file from uri

File mFile = new File(new URI(mediaUri.toString()));

The mediaUri.toString() was not returning the correct path of the video file from to get the correct path of the video file

File mFile = new File(new URI(getRealPathFromURI(contentUri));


     public String getRealPathFromURI(Uri contentUri) {

    String[] proj = { MediaStore.Video.Media.DATA };
    Cursor cursor = managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

This answer is userfull while fetching the video file path from the given Uri

The problem is, that uri.toString() makes string of device (file:) and path itself. You should use uri.getPath() instead. This returns not Path, but String though. With maybe extra / or \\ at start. You should give it off so that it would work on all systems. The correct and OS-independent line will be:

File mFile = new File(new Path(mediaUri.getPath()).toString);

Works on our project on win, mac, linux.

A couple of things could be wrong.

First, mediaUri could be null

Second, is readFile your own method(I guess it is), then you should either post the method or do your try/catch in readFile.
The reason is this. If I am to go by your code, you are creating a new file that should not contain any data, so what are you using readFile for? If the file exists, and mediaUri is not null, then the null exception is from your readFile method.

You should check mediaURI were corrected file path, I suppose it's not corrected file path.

Could you please explain a bit more???

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM