在我的应用程序中,我让用户选择一张图片并在之后裁剪。在此之后,图像被设置为imageView。
我的解决方案没有任何问题,直到我昨天收到两个华为智能手机有相同问题的消息。他们总是收到一条消息"无法加载图片......"并且没有图像设置到imageView。
在调试时,我认识到,对于裁剪意图,onActivityResult中的data.getData()为空。
我已经尝试了很多不同的选择和裁剪图像的可能性,所有这些都适合我,但华为智能手机没有。我没有想法,可能需要一些帮助。
以下是代码:
public void onClick(View v){
Permissions.verifyStoragePermissions(this);
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
Uri uri = data.getData();
this.performCrop(uri);
} else if (requestCode == RESULT_CROP_IMG) {
if (data != null) {
ImageView imgView =
(ImageView) findViewById(R.id.imageView_my_profile);
imgView.setImageURI(data.getData());
}
}
} catch (Exception e) {
Toast.makeText(this, getString(R.string.global_default_error), Toast.LENGTH_LONG)
.show();
}
}
private void performCrop(Uri picUri) {
try {
ImageView imgView = (ImageView) findViewById(R.id.imageView_my_profile);
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", imgView.getWidth());
cropIntent.putExtra("aspectY", imgView.getHeight());
cropIntent.putExtra("outputX", imgView.getWidth());
cropIntent.putExtra("outputY", imgView.getHeight());
String folder_main = "Test";
File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), folder_main);
f.mkdirs();
f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString()+"/Test/profilepic" + System.currentTimeMillis() +".jpg");
Uri newUri = Uri.fromFile(f);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, newUri);
startActivityForResult(cropIntent, RESULT_CROP_IMG);
}
catch (ActivityNotFoundException anfe) {
ImageView imgView = (ImageView) findViewById(R.id.imageView_my_profile);
imgView.setImageURI(picUri);
}
}