I have the below code for which is working in android 6 but while using it in android 7+ the application crashes with nullpointerexception.
Intent intentfile = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intentfile, SELECT_PHOTO);
and in the activityresult the code is
case SELECT_PHOTO:
if (resultCode == RESULT_OK) {
selectedImage = data.getData();
Picasso.get().load(selectedImage).fit().centerCrop().into(imageView);
imgselect.setVisibility(Button.INVISIBLE);
relativeLayout.setVisibility(RelativeLayout.VISIBLE);
upload.setVisibility(Button.VISIBLE);
next code is of uploading the selectedimage to server
String sourcepath = getRealPathFromURI(this, selectedImage);
final String filename = sourcepath.substring(sourcepath.lastIndexOf("/") + 1);
final String destinationpath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DiaryApp/Images/";
copyFile(sourcepath, filename, destinationpath);
imageRef = storageRef.child(firebaseUser.getUid() + "/Images/" + filename);
//creating and showing progress dialog
progressDialog = new ProgressDialog(this);
progressDialog.setMax(100);
progressDialog.setMessage("Uploading...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show();
progressDialog.setCancelable(false);
//starting upload
uploadTask = imageRef.putFile(selectedImage);
// Observe state change events such as progress, pause, and resume
uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
//sets and increments value of progressbar
progressDialog.incrementProgressBy((int) progress);
}
});
// Register observers to listen for when the download is done or if it fails
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
Toast.makeText(view.getContext(), "Error in uploading!", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
Uri downloadUrl = taskSnapshot.getDownloadUrl();
Toast.makeText(view.getContext(), "Upload successful", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
//showing the uploaded image in ImageView using the download url
Image image = new Image(editText.getText().toString(), "Image", "file:" + destinationpath + filename, downloadUrl + "", getDate(), getTime());
userDataRef.child("data").child(userDataRef.push().getKey()).setValue(image);
finish();
now the method getRealPathFromUri(this,selectedImage);
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
if(contentUri.toString().contains("images")) //Error is here in this line {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
else if(contentUri.toString().contains("video")){
String[] proj = {MediaStore.Video.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
else {
return "Empty";
}
}
finally {
if (cursor != null) {
cursor.close();
}
}
}
the selectedimage is null in android 7+ while in lesser android version it is working?