0
我在Android中有一个PopupWindow,并且里面有一个imageview。当我按下图像视图时,我想打开相机拍摄照片,当我回过头来为imageview背景设置照片时。打开相机拍照并将其设置在PopupWindow中的图像视图 - android
问题是,当我复出(onActivityResult),则popupwindow是驳回(),并且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。
我不得不说,我已经测试了我的代码在三星S5的Android 5.0,它工作。但在其他设备上.. – hackingforgirls
你没有真正解释你的问题,所以我假设getBitmap方法返回null,是吗? –
不! mCurrentPhotoPath为null,但我使用共享的prefs来保存并重新设置它的值,所以没关系。它不会更改iMageVie中的图片,这是我的问题。但在S5上,我测试过了,它正在工作...... headchache .... – hackingforgirls