All of a sudden the program has stopped working.
I have a URI: "content://com.android.providers.media.documents/document/image%3A13", a file path to an image.
The path for the URI is chosen like so:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK) {
try {
// This bit here
Bitmap bitmap = getPath(data.getData());
Log.i("Bitmap", "Bmp: " + data.getData());
}catch (Exception e){
Log.e("Error", "Error with setting the image.");
e.printStackTrace();
}
}
}
So, getPath() is called, putting the data in as a URI (URI is correct, the log shows that)
In getPath():
private Bitmap getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null,null);
cursor.moveToFirst();
// it is this line here, it returns null for some reason.
String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
// Convert file path into bitmap image using below line.
Log.i("File Path", "File name: " + filePath); // this comes out as NULL in the logcat
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
filePathForUpload = filePath;
try {
ExifInterface exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
bitmap = rotateBitmap(bitmap, orientation);
}catch (Exception e){
Log.d("Error", "error with bitmap!");
e.printStackTrace();
}
return bitmap;
}
Logcat output:
java.lang.IllegalArgumentException: filename cannot be null
at android.media.ExifInterface.<init>(ExifInterface.java:121)
at build.com.build.SubmitPicActivity.getPath(SubmitPicActivity.java:123)
at build.com.build.SubmitPicActivity.onActivityResult(SubmitPicActivity.java:93)
at android.app.Activity.dispatchActivityResult(Activity.java:5423)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Line 123 is : String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
And line 93 is: Bitmap bitmap = getPath(data.getData());
Any suggestions?
A Uri is not necessarily a File. The code that you are using to try to get a File for a Uri was never reliable and will not work much going forward.
Please consume the Uri appropriately, using a ContentResolver and methods like openInputStream() and getType(). In essence, you treat a Uri the same way that you would a URL to a Web server, and for much the same reason: there is no requirement for the content surfaced by a ContentProvider to be coming from an ordinary File that your app can access.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With