0

I have looked - and I cannot find anything else specific to the URI relating to this issue.

My app allows the user to select an image from gallery, the app then receives the uri, converts it to bitmap, then compresses it to a JPEG and stores the file in cacheDir for later usage. After selecting the image, the app "Stops working".

I am aware adding resultCode != RESULT_CANCELED, and checking if data is null will likely fix the initial crash, but I don't think that will solve the overall problem - whatever that may be. I'm still learning java - I was stiched up by a developer so am being forced to make my app myself so doing my best to learn, if there's anything I can improve in the code please let me know, thanks.

Select image from gallery:

public void selectImageFromGallery(){
    Intent chooserIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(chooserIntent, 1);
}

Receive the data, get URI in activity result, send to net function:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK && requestCode == IMAGE_PICK  || requestCode == VIDEO_PICK) {
        switch (requestCode) {
            case 1:
                Intent intent = getIntent();
                setResult(RESULT_OK, intent);
                Uri imageUri = intent.getData();
                try {
                    startSendImage(imageUri);
                } catch (IOException e) {
                    e.printStackTrace();
                    return;
                }


                //Toast.makeText(this, "Sending uri to startSendImage", Toast.LENGTH_SHORT).show();
                //uriData = data.getData();
                //startSendImage(uriData);

                break;

            case 2:


                break;
            default:
                break;
        }
    }
}

Prepare the image for later usage:

public void startSendImage (Uri imageUri) throws IOException {
    Toast.makeText(this, "Received. Copying image.", Toast.LENGTH_SHORT).show();
    //Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);

//create a file to write bitmap data
    String filename = "temp.jpg";
    File f = new File(this.getCacheDir(), filename);
    f.createNewFile();

//Convert bitmap to byte array
    //Bitmap bitmap1 = bitmap;
    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100 /*ignored for PNG*/, bos);
    byte[] bitmapdata = bos.toByteArray();

//write the bytes in file
    FileOutputStream fos = new FileOutputStream(f);
    fos.write(bitmapdata);
    fos.flush();
    fos.close();
    if (f.exists()){
        Toast.makeText(this, "File Exists", Toast.LENGTH_SHORT).show();
    }
}

The logcat:

2019-04-06 18:20:43.907 18116-18116/com.com.project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.com.project, PID: 18116
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/1598 flg=0x1 (has extras) }} to activity {com.com.project/com.com.project.MainActivity}: java.lang.NullPointerException: uri
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4491)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4534)
    at android.app.ActivityThread.-wrap20(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1752)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6944)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
 Caused by: java.lang.NullPointerException: uri
    at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:128)
    at android.content.ContentResolver.openInputStream(ContentResolver.java:956)
    at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:952)
    at com.com.project.MainActivity.startSendImage(MainActivity.java:138)
    at com.com.project.MainActivity.onActivityResult(MainActivity.java:102)
    at android.app.Activity.dispatchActivityResult(Activity.java:7556)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4487)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4534) 
    at android.app.ActivityThread.-wrap20(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1752) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6944) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – user2864740 Apr 06 '19 at 17:42
  • You are getting a `null` `Uri` from `getData()`. At minimum, change `resultCode == RESULT_OK && requestCode == IMAGE_PICK || requestCode == VIDEO_PICK` to `resultCode == RESULT_OK && (requestCode == IMAGE_PICK || requestCode == VIDEO_PICK)`. Also, change your `startActivityForResult()` call to use `IMAGE_PICK` instead of `1`. – CommonsWare Apr 06 '19 at 17:42
  • @CommonsWare thanks, I've made that edit - the error still persist though.. – Tiền Matty Apr 06 '19 at 17:46
  • I usually see people using `EXTERNAL_CONTENT_URI` rather than `INTERNAL_CONTENT_URI`. Beyond that... is it actually letting you pick an image? – CommonsWare Apr 06 '19 at 17:48
  • @CommonsWare good point, I changed it to internal to test if that changed anything - changed it back now. But yes, either way it was opening the gallery, but as soon as I picked the image it would "Stop working". – Tiền Matty Apr 06 '19 at 18:12
  • I have no idea why you would get a `null` `Uri`, other than by having a broken gallery app on your device. If you log `intent.getData()`, it shows up as `null`? – CommonsWare Apr 06 '19 at 18:14
  • @CommonsWare I found the issue, I was creating a new intent, intent, and getting data from that, instead of getting the data from the activity's data lol.. but now when sending the image to next task, Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.com.project/cache/temp.jpg – Tiền Matty Apr 06 '19 at 18:36
  • "I was creating a new intent, intent, and getting data from that" -- I should have noticed that, sorry! In terms of your follow-on problem, you have an issue with your `FileProvider` configuration. If you cannot identify what is going wrong, open up another Stack Overflow question, where your [mvce] includes your `FileProvider` XML resource and your code that is calling `getUriForFile()`. – CommonsWare Apr 06 '19 at 18:37
  • @CommonsWare Me too, not a problem! Also, I managed to fix the file provider by changing my – Tiền Matty Apr 06 '19 at 20:03

0 Answers0