java - cursor.getString returns null with a valid uri -
all of sudden program has stopped working.
i have uri: "content://com.android.providers.media.documents/document/image%3a13
", file path image.
the path uri chosen so:
protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if(resultcode == result_ok) { try { // bit here bitmap bitmap = getpath(data.getdata()); log.i("bitmap", "bmp: " + data.getdata()); }catch (exception e){ log.e("error", "error setting image."); e.printstacktrace(); } } }
so, getpath()
called, putting data in uri (uri correct, 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(); // line here, returns null reason. string filepath = cursor.getstring(cursor.getcolumnindex(mediastore.images.media.data)); // convert file path bitmap image using below line. log.i("file path", "file name: " + filepath); // comes out null in 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 bitmap!"); e.printstacktrace(); } return bitmap; }
logcat output:
java.lang.illegalargumentexception: filename cannot null @ android.media.exifinterface.<init>(exifinterface.java:121) @ build.com.build.submitpicactivity.getpath(submitpicactivity.java:123) @ build.com.build.submitpicactivity.onactivityresult(submitpicactivity.java:93) @ android.app.activity.dispatchactivityresult(activity.java:5423) @ android.app.activitythread.deliverresults(activitythread.java:3361) @ android.app.activitythread.handlesendresult(activitythread.java:3408) @ android.app.activitythread.access$1300(activitythread.java:135) @ android.app.activitythread$h.handlemessage(activitythread.java:1244) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5017) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) @ dalvik.system.nativestart.main(native method)
line 123 : string filepath = cursor.getstring(cursor.getcolumnindex(mediastore.images.media.data));
and line 93 is: bitmap bitmap = getpath(data.getdata());
any suggestions?
a uri
not file
. code using try file
uri
never reliable , not work going forward.
please consume uri
appropriately, using contentresolver
, methods openinputstream()
, gettype()
. in essence, treat uri
same way url web server, , same reason: there no requirement content surfaced contentprovider
coming ordinary file
app can access.
Comments
Post a Comment