Ich rufe Activity.requestPermissions (READ_EXTERNAL_STORAGE) auf und nachdem Benutzer auf ALLOW klicken, versuche ich BitmapFactory.decodeFile (Pfad) zu verwenden, aber es gibt null zurück. Und wenn die Erlaubnis bereits erteilt ist alles in Ordnung. Was ist los mit dir?Android BitmapFactory.decodeFile gibt Null zurück
ImageChooser Klasse
public class ImageChooser {
private final static int GET_IMAGE_REQUEST = 1;
private final static int PERMISSIONS_REQUEST = 2;
public interface Callback {
void onImageChosen(Bitmap image);
}
private Activity activity;
private Fragment fragment;
private Callback callback;
private String capturedImagePath;
public ImageChooser(Activity activity, Fragment fragment, Callback callback) {
this.activity = activity;
this.fragment = fragment;
this.callback = callback;
}
public void choose() {
boolean readStorageGranted = PermissionsManager
.isPermissionGrantedReal(activity, Permission.READ_EXTERNAL_STORAGE);
boolean cameraGranted = PermissionsManager
.isPermissionGrantedReal(activity, Permission.CAMERA);
if (readStorageGranted && cameraGranted) {
chooseImage();
} else {
fragment.requestPermissions(new String[] {
Permission.CAMERA.permission,
Permission.READ_EXTERNAL_STORAGE.permission
}, PERMISSIONS_REQUEST);
}
}
private void chooseImage() {
Intent pickIntent = new Intent(Intent.ACTION_PICK);
pickIntent.setType("image/*");
String filename = "image.png";
String dir = activity.getExternalCacheDir().getAbsolutePath();
capturedImagePath = dir + "/" + filename;
File file = new File(capturedImagePath);
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
Intent chooserIntent = Intent.createChooser(pickIntent,
activity.getString(R.string.take_choose_photo));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { captureIntent });
fragment.startActivityForResult(chooserIntent, GET_IMAGE_REQUEST);
}
public void handleOnActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GET_IMAGE_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap image = null;
String picturePath = null;
if (data == null) { // From Camera.
File imageFile = new File(capturedImagePath);
if (imageFile.exists()) {
image = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
}
} else { // From Gallery.
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = activity.getContentResolver().query(selectedImage, filePathColumn,
null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
image = BitmapFactory.decodeFile(picturePath);
}
}
if (image != null) {
callback.onImageChosen(image);
}
}
}
public void handleOnRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
if (grantResults.length > 0 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED &&
grantResults[1] == PackageManager.PERMISSION_GRANTED) {
chooseImage();
}
}
}
Beispiel des Verwendens
public class CustomizeFragment extends Fragment {
@Bind(R.id.add_image)
View addImageView;
@Bind(R.id.switch_button_off)
Button switchOffButton;
@Bind(R.id.switch_button_on)
Button switchOnButton;
@Bind(R.id.mb_image)
RoundedImageView imageView;
private ImageChooser imageChooser;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_customize, container, false);
ButterKnife.bind(this, v);
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
imageChooser = new ImageChooser(getActivity(), this, image -> {
addImageView.setVisibility(View.GONE);
imageView.setVisibility(View.VISIBLE);
imageView.setImageBitmap(image);
});
}
@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);
}
@OnClick({R.id.add_image, R.id.mb_image})
void onAddImageClick() {
imageChooser.choose();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
imageChooser.handleOnActivityResult(requestCode, resultCode, data);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
imageChooser.handleOnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
Bitte geben Sie eine [mcve] an. – CommonsWare
Bitte poste deinen Code. – sonngaytho
Ich habe meinen Code hinzugefügt – xskit