我正在创建一个应用程序,可以选择从相机或图库中获取图像并将其显示在ImageView中。 我从Android Developers Tutorial.得到了这个 我正在使用DialogInterface:
private void selectImage() {
final CharSequence[] items = { "Take Photo", "Choose from Library",
"Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(ReportIncident.this);
builder.setTitle("Add Picture");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
try {
photoFile = createImageFile();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, 0);
} catch (IOException ex) {
// Error occurred while creating the File
ex.printStackTrace();
}
}
} else if (items[item].equals("Choose from Library")) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"), 1);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
createImageFile创建将保存照片的目录/文件:
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "FIREFLOOD_" + 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();
return image;
}
“从图书馆中选择”部分已经有效,但我对“拍照”部分有疑问。特别是在这一行:
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
galleryAddPic();
startActivityForResult(takePictureIntent, 0);
在startActivityOnResult()之前使用putExtra()和galleryAddPic() 在拍摄照片后崩溃应用程序但成功保存图像 画廊。但是从代码中删除这两种方法会显示拍摄的照片 到ImageView reportImage。
startActivityForResult显示通过activityOnResult拍摄到ImageView的图像:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 1) {
onSelectFromGalleryResult(data);
}else if (requestCode == 0) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
reportImage.setImageBitmap(imageBitmap);
}
}
}
将图像保存到图库是由galleryAddPic:
执行的private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
问题是我在哪里可以放这两种方法(putExtra和 galleryAddPic)使这两个功能有效:
- 显示拍摄到ImageView的照片
- 同时将照片保存到图库。
醇>我不能完全做这两件事。请帮忙。我试着把 onAcitivityResult上的putExtra和galleryAddPic但它仍然存在 崩溃。如果没有putExtra,galleryAddPic将无效。
答案 0 :(得分:1)