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