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(
"MyImages");
storageDir.mkdirs();
// File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
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;
}
static final int REQUEST_TAKE_PHOTO = 1;
private void dispatchTakePictureIntent() {
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
photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
//...
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoFile));
//takePictureIntent = getIntent().putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
if(data != null) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
main.xml中
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.59"
android:text="File Uri" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="0.52"
android:maxHeight="@dimen/max_image_height"
android:src="@drawable/ic_launcher" />
嗨,我想拍照并保存到我创建的文件“MyImages”,我想查看照片imageview。我可以拍照并保存,但我不能用imageview查看。 putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoFile));由于Uri.fromFile(photoFile),数据为空。请帮助!
答案 0 :(得分:0)
如果您指定了MediaStore.EXTRA_OUTPUT,则所拍摄的图像将被写入该路径,并且不会向onActivityResult提供任何数据。您可以从您指定的内容中读取图像。
在此处查看另一个解决的相同问题:Android Camera:数据意图返回null