when ever i am not selecting any image after opening the gallery and goin back my app crashes. plz help me out guys. this is a program to upload image to server selecting from a
You can handle it this way:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
//your code
}
else {
//alert of "No image was selected"
}
}
Don't perform any operation on data
if its null. So it won't give a crash.
Hope it helps.
Add below line in your code. If the operation is cancelled, you will not get RESULT_OK
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;
Try this, add if(resultCode == RESULT_OK)
:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK){ //<-- Add this
if (requestCode == 1 ) {
//Bitmap photo = (Bitmap) data.getData().getPath();
Uri selectedImageUri = data.getData();
imagepath = getPath(selectedImageUri);
Bitmap bitmap=BitmapFactory.decodeFile(imagepath);
imageview.setImageBitmap(bitmap);
// messageText.setText("Uploading file path:" +imagepath);
}
}
}
In your current code , even if the result code is not OK
its executing data.getData()
which is null