在我的Android应用程序中,我想从图库中选择一个图像并通过(ASCII TABLE)压缩器压缩它,然后将该压缩图像上传到firebase存储中。 当我从图库中选择任何图像时,应用程序突然崩溃并出现以下异常。
尝试在空对象引用上调用虚拟方法'int android.graphics.Bitmap.getWidth()'
我的代码
调用图库意图(给出所有运行时权限)
public void selectImage(){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent.createChooser(intent,"Select Picture"),IMAGE_SELECTION);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == IMAGE_SELECTION && resultCode == Activity.RESULT_OK){
Uri uri = data.getData();
imagePath = uri.getPath();
if(imagePath.equals("")){
Toast.makeText(getContext(), getString(R.string.imageNotSelected), Toast.LENGTH_SHORT).show();
}else{
MembersData.uploadImage(imagePath,Helper.getPhone(getContext()),profileProgressbar,getContext(), profileImage);
}
}
}
下面是uploadImage()方法: -
public static void uploadImage(String imagePath, String enroll, final ProgressBar mProgressbar, final Context context, final CircleImageView imgView){
DatabaseReference myRoot = FirebaseDatabase.getInstance().getReference();
final DatabaseReference currentMember = myRoot.child(MEMBERS).push();
StorageReference myRootStorage = FirebaseStorage.getInstance().getReference();
StorageReference memberImages = myRootStorage.child(enroll);
File imageFile = new File(imagePath);
File compressedImageFile = null;
try {
compressedImageFile = new Compressor(context).compressToFile(imageFile);
} catch (IOException e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
}
memberImages.putFile(Uri.fromFile(compressedImageFile)).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String downloadUri = String.valueOf(taskSnapshot.getDownloadUrl());
Picasso.with(context).load(downloadUri).into(imgView);
currentMember.child(PROFILE_IMG).setValue(downloadUri);
if(mProgressbar.getVisibility()== VISIBLE){
mProgressbar.setVisibility(INVISIBLE);
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
if(mProgressbar.getVisibility()== VISIBLE){
mProgressbar.setVisibility(INVISIBLE);
}
}
});
}