我在Android中有一个PopupWindow,在里面我有一个imageview。当我按下imageview时,我想打开相机拍照,当我回来为imageview的背景设置照片时。
问题是当我回归(onActivityResult)时,popupwindow是dismiss(),而imageView背景是默认的。
ImageView的@的onClick:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.i(TAG, "IOException");
}
// Continue only if the File was successfully created
if (photoFile != null) {
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
createImageFile()函数
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, // prefix
".jpg", // suffix
storageDir // directory
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("picture_path", mCurrentPhotoPath);
editor.commit();
return image;
}
@onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
try {
prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
mCurrentPhotoPath = prefs.getString("picture_path", null);
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
add_photo.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
我使用Sharedpreferences来避免 mCurrentPhotoPath 上的null。
答案 0 :(得分:0)
假设问题是MediaStore.Images.Media.getBitmap返回null,那是因为它用于从某些ContentResolver中的某个应用程序处理的URL中检索图像,这在您的实现中并非如此
在您的实施中,您将图像存储在设备文件系统上,您可以使用返回位图的BitmapFactorys decodeFile方法检索该文件系统。