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)