如何正确地将照片写入内存并获取URI(Honeycomb)?
我正在构建一个可以拍照然后通过电子邮件发送的应用程序。为此,我必须将其保存到设备的内存中。我遇到的问题是,当应用程序写入我的文件时,它会在这行特定的代码处崩溃:
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(Environment.getExternalStorageDirectory()));
降低你拥有整个函数和代码:
private void takePicture(){
File outputFile = new File(Environment.getExternalStorageDirectory(), "image.jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(Environment.getExternalStorageDirectory()));
startActivityForResult(intent, 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == CAMERA_PIC_REQUEST && resultCode == Activity.RESULT_OK){
picture = (Bitmap) data.getExtras().get("data");
pictureView.setImageBitmap(picture);
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "Picture");
values.put(Images.Media.BUCKET_ID, "picture_ID");
values.put(Images.Media.DESCRIPTION, "");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
pictureUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
OutputStream outstream;
try{
outstream = getContentResolver().openOutputStream(pictureUri);
picture.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
outstream.close();
}catch(FileNotFoundException e){
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
如果我注释掉该行,应用程序就不会再崩溃。我需要获取照片的 URI,以便通过电子邮件发送。
如果有经验丰富的人提供帮助,我们将不胜感激。
I'm building an app that takes a photo and then e-mails it. For this I have to save it into the device's memory. The problem I'm getting is that while the app writes my file, it crashes at this particular line of code:
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(Environment.getExternalStorageDirectory()));
Lower you have the entire function and code:
private void takePicture(){
File outputFile = new File(Environment.getExternalStorageDirectory(), "image.jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(Environment.getExternalStorageDirectory()));
startActivityForResult(intent, 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == CAMERA_PIC_REQUEST && resultCode == Activity.RESULT_OK){
picture = (Bitmap) data.getExtras().get("data");
pictureView.setImageBitmap(picture);
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "Picture");
values.put(Images.Media.BUCKET_ID, "picture_ID");
values.put(Images.Media.DESCRIPTION, "");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
pictureUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
OutputStream outstream;
try{
outstream = getContentResolver().openOutputStream(pictureUri);
picture.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
outstream.close();
}catch(FileNotFoundException e){
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
If I comment that line out, the app doesn't crash anymore. I need to get the photo's URI so I can then email it.
Any help from you guys more experienced out there would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个
Try this