0

When I execute this code it works fine in my emulator (even saves the image), but it closes the application on my phone (nexus 5)

When I set a try catch around the Uri line the program does not crash (but then again its null value could also have no influence on the code below it)

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode== 0 && resultCode == Activity.RESULT_OK) {
        Bitmap x = (Bitmap) data.getExtras().get("data");
        userImage.setImageBitmap(x);
        ContentValues values = new ContentValues();
        values.put(Images.Media.TITLE, "profileimage");
        values.put(Images.Media.BUCKET_ID, "profileimage");
        values.put(Images.Media.DESCRIPTION, "profile Image");
        values.put(Images.Media.MIME_TYPE, "image/jpeg");
        Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
        OutputStream outstream;
        try {
            outstream = getContentResolver().openOutputStream(uri);
            x.compress(Bitmap.CompressFormat.JPEG, 70, outstream);
            outstream.close();
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
    }
}

Cheers!

EDIT: (sorry for my inactivity - new android phone that logs :D)

This is the error log:

03-28 15:33:51.230: E/Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/ext ActivityFilt[ 03-28 15:33:51.231 10005:10005 D/AndroidRuntime(10005): Shutting down VM
03-28 15:33:51.238: E/AndroidRuntime(10005): FATAL EXCEPTION: main
03-28 15:33:51.238: E/AndroidRuntime(10005): Process: com.td.tdabslidingmenu, PID: 10005
03-28 15:33:51.238: E/AndroidRuntime(10005): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.td.tdabslidingmenu/com.td.tdabslidingmenu.MainActivity}: java.lang.NullPointerException: uri
03-28 15:33:51.238: E/AndroidRuntime(10005):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
03-28 15:33:51.238: E/AndroidRuntime(10005):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
03-28 15:33:51.238: E/AndroidRuntime(10005):    at android.app.ActivityThread.-wrap16(ActivityThread.java)
03-28 15:33:51.238: E/AndroidRuntime(10005):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
03-28 15:33:51.238: E/AndroidRuntime(10005):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-28 15:33:51.238: E/AndroidRuntime(10005):    at android.os.Looper.loop(Looper.java:148)
03-28 15:33:51.238: E/AndroidRuntime(10005):    at android.app.ActivityThread.main(ActivityThread.java:5417)
03-28 15:33:51.238: E/AndroidRuntime(10005):    at java.lang.reflect.Method.invoke(Native Method)
03-28 15:33:51.238: E/AndroidRuntime(10005):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-28 15:33:51.238: E/AndroidRuntime(10005):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-28 15:33:51.238: E/AndroidRuntime(10005): Caused by: java.lang.NullPointerException: uri
03-28 15:33:51.238: E/AndroidRuntime(10005):    at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:60)
03-28 15:33:51.238: E/AndroidRuntime(10005):    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:922)
03-28 15:33:51.238: E/AndroidRuntime(10005):    at android.content.ContentResolver.openOutputStream(ContentResolver.java:702)
03-28 15:33:51.238: E/AndroidRuntime(10005):    at android.content.ContentResolver.openOutputStream(ContentResolver.java:678)
03-28 15:33:51.238: E/AndroidRuntime(10005):    at com.td.tdabslidingmenu.MainActivity.onActivityResult(MainActivity.java:263)
03-28 15:33:51.238: E/AndroidRuntime(10005):    at android.app.Activity.dispatchActivityResult(Activity.java:6456)
03-28 15:33:51.238: E/AndroidRuntime(10005):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
03-28 15:33:51.238: E/AndroidRuntime(10005):    ... 9 more
  • 3
    **Never** create `catch` blocks that eat exceptions that you are trying to diagnose. Use `Log.e()` methods to log the exceptions, then use LogCat to examine your Java stack traces: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Jan 12 '16 at 18:20
  • ..And please add the stack trace to your question, so we can diagnose your problem and help you fix it. – Jonas Czech Jan 12 '16 at 18:32
  • @CommonsWare Well I only used the catch to show that the problem occurs there (it isnt in the code but would be around the "Uri uri = ..." line & the problem only occurs on the nexus so ... dont have any direct console/logger on it :/ – goodstuff20 Jan 12 '16 at 19:04
  • & @JonasCz (also above :D) – goodstuff20 Jan 12 '16 at 19:08
  • 3
    "the problem only occurs on the nexus so ... dont have any direct console/logger on it" -- did somebody steal your USB cable? If so, it would be a worthwhile investment to replace it. Not only can you use it to charge the phone, but you can use it for development purposes: to run your app on the device, to examine LogCat, to establish breakpoints and otherwise use a debugger, etc. – CommonsWare Jan 12 '16 at 19:10
  • Yes, the only way to fix this is to get the LogCat from the device it's crashing on, in this case the Nexus.. – Jonas Czech Jan 12 '16 at 19:26
  • ah true sorry - device is always offline in adb nomatter what & no root -.- so this the only way? :D – goodstuff20 Jan 12 '16 at 20:32
  • You don't need root to use adb via a usb connection. Have you enabled developer options by tapping 7 times on the build number in settings? Only other step is verifying you've got a proper usb driver installed for your device and you should be good to go. – Michael Krause Jan 12 '16 at 22:41
  • @Michael Krause yes I've done all that - I mentioned the root for using apps because the adp didn't seem to work (seemed to have installed proper drivers & no exclamation mark & ive got usb debugging activated but still adb devices -> ... Offline :/ – goodstuff20 Jan 13 '16 at 07:30

1 Answers1

0

Which Android version is your Nexus 5 running? If it's Marshmallow (or the N preview), chances are it's a permission problem caused by the Media.EXTERNAL_CONTENT_URI part of your code.

Daniel Veihelmann
  • 1,275
  • 10
  • 13
  • Yes it is Marshmallow & thanks! Would I have to do a grantUriPermissions provider or..? (Sorry im still rather new to the android field) – goodstuff20 Mar 29 '16 at 20:10