我想将从相机返回的图像设置为CropImageView。 使用的库是this。
图库返回的图像已正确设置。
但是对于相机返回的图像,当设置为setImageUri()
时,ImageView为空,尽管它在日志中显示图像的Uri
。
登录onActivityResult
文件:///storage/emulated/0/Android/data/com.android.example/cache/pickImageResult.jpeg
尝试使用setImageBitmap()
进行设置时,Bitmap photo = (Bitmap) data.getExtras().get("data")
会产生NullPointerException
。
代码是
public class ImagePickerActivity extends AppCompatActivity {
private CropImageView mCropImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_picker);
Log.d("image picker", "activity created");
mCropImageView = (CropImageView) findViewById(R.id.cropImageView);
}
/**
* On load image button click, start pick image chooser activity.
*/
public void onLoadImageClick(View view) {
startActivityForResult(getPickImageChooserIntent(), 200);
}
/**
* Crop the image and set it back to the cropping view.
*/
public void onCropImageClick(View view) {
Bitmap cropped = mCropImageView.getCroppedImage(500, 500);
if (cropped != null)
mCropImageView.setImageBitmap(cropped);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
Uri imageUri = getPickImageResultUri(data);
Log.d("image picker", imageUri.toString());
/*Bitmap photo = (Bitmap) data.getExtras().get("data");
mCropImageView.setImageBitmap(photo );*/
mCropImageView.setImageUri(imageUri);
}
}
/**
* Create a chooser intent to select the source to get image from.<br/>
* The source can be camera's (ACTION_IMAGE_CAPTURE) or gallery's (ACTION_GET_CONTENT).<br/>
* All possible sources are added to the intent chooser.
*/
public Intent getPickImageChooserIntent() {
// Determine Uri of camera image to save.
Uri outputFileUri = getCaptureImageOutputUri();
List<Intent> allIntents = new ArrayList<>();
PackageManager packageManager = getPackageManager();
// collect all camera intents
Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(res.activityInfo.packageName);
if (outputFileUri != null) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
}
allIntents.add(intent);
}
// collect all gallery intents
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
List<ResolveInfo> listGallery = packageManager.queryIntentActivities(galleryIntent, 0);
for (ResolveInfo res : listGallery) {
Intent intent = new Intent(galleryIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(res.activityInfo.packageName);
allIntents.add(intent);
}
// the main intent is the last in the list so pickup the useless one
Intent mainIntent = allIntents.get(allIntents.size() - 1);
for (Intent intent : allIntents) {
if (intent.getComponent().getClassName().equals("com.android.documentsui.DocumentsActivity")) {
mainIntent = intent;
break;
}
}
allIntents.remove(mainIntent);
// Create a chooser from the main intent
Intent chooserIntent = Intent.createChooser(mainIntent, "Select source");
// Add all other intents
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, allIntents.toArray(new Parcelable[allIntents.size()]));
return chooserIntent;
}
/**
* Get URI to image received from capture by camera.
*/
private Uri getCaptureImageOutputUri() {
Uri outputFileUri = null;
File getImage = getExternalCacheDir();
if (getImage != null) {
outputFileUri = Uri.fromFile(new File(getImage.getPath(), "pickImageResult.jpeg"));
}
return outputFileUri;
}
/**
* Get the URI of the selected image from {@link #getPickImageChooserIntent()}.<br/>
* Will return the correct URI for camera and gallery image.
*
* @param data the returned data of the activity result
*/
public Uri getPickImageResultUri(Intent data) {
boolean isCamera = true;
if (data != null) {
String action = data.getAction();
isCamera = action != null && action.equals(MediaStore.ACTION_IMAGE_CAPTURE);
}
return isCamera ? getCaptureImageOutputUri() : data.getData();
}
}
请帮助解决此问题。提前致谢。 代码取自here。
答案 0 :(得分:0)
为了从相机获取图像我使用下面的代码,它在棒棒糖中工作:
private void captureFromCamera()
Calendar calendar = Calendar.getInstance();
String fileName = String.valueOf(calendar.getTimeInMillis());
fileName += ".jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,
"Image capture by camera");
Uri imageUri = activity.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
new DevicePreferences().addKey(activity, Constants.IMAGE_URI,
imageUri.toString()); // Storing in shared preference and in onActivityResult getting uri from shared preference
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent,
CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
答案 1 :(得分:-1)
可以替换此代码
Uri imageUri = getPickImageResultUri(data);
Log.d("image picker", imageUri.toString());
mCropImageView.setImageUri(imageUri);
用..这个,
Uri imageUri = data.getData();
path = GetPathFromURI(context, imageUri);
bitmap = BitmapFactory.decodeFile(path);
mCropImageView.setImageBitmap(bitmap);
<强> GetPathFromURI 强>
public static String GetPathFromURI(Context context,Uri contentUri) {
String realPath = "";
String[] projection = { MediaStore.Images.Media.DATA };
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(contentUri, projection, null, null, null);
if (cursor.moveToFirst()) {
realPath = cursor.getString(0);
}
cursor.close();
return realPath;
}