Create file from Uri exception occurs
While开发者_JAVA技巧 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
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.
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.
You should check mediaURI were corrected file path, I suppose it's not corrected file path.
Could you please explain a bit more???
精彩评论