将照片保存到文件时出现问题 [英] Problems saving a photo to a file
问题描述
伙计,当我发送一个要求拍照的意图时,我仍然无法保存图片.这就是我正在做的事情:
Man, I am still not able to save a picture when I send an intent asking for a photo to be taken. Here's what I am doing:
创建一个表示路径名的 URI
Make a URI representing the pathname
android.content.Context c = getApplicationContext();
String fname = c.getFilesDir().getAbsolutePath()+"/parked.jpg";
java.io.File file = new java.io.File( fname );
Uri fileUri = Uri.fromFile(file);
创建 Intent(不要忘记 pkg 名称!)并开始活动
Create the Intent (don't forget the pkg name!) and start the activity
private static int TAKE_PICTURE = 22;
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra("com.droidstogo.boom1." + MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult( intent, TAKE_PICTURE );
相机活动开始,我可以拍照并批准它.我的 onActivityResult()
然后被调用.但是我的文件没有被写入.URI 为:file:///data/data/com.droidstogo.boom1/files/parked.jpg
The camera activity starts, and I can take a picture, and approve it. My onActivityResult()
then gets called. But my file doesn't get written.
The URI is: file:///data/data/com.droidstogo.boom1/files/parked.jpg
我可以创建缩略图 OK(通过不将额外内容放入 Intent),并且可以写入该文件 OK,然后再将其读回.
I can create thumbnail OK (by not putting the extra into the Intent), and can write that file OK, and later read it back).
谁能看出我犯了什么简单的错误?logcat 中没有任何明显的显示 - 相机显然正在拍照.谢谢,
Can anyone see what simple mistake I am making? Nothing obvious shows up in the logcat - the camera is clearly taking the picture. Thanks,
彼得
我应该提到我在 AndroidManifest.xml 文件中设置了适当的权限:
I should mention that I have the appropriate permissions set in the AndroidManifest.xml file:
<uses-permission android:name="android.permission.READ_OWNER_DATA" />
<uses-permission android:name="android.permission.WRITE_OWNER_DATA" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-library android:name="com.google.android.maps" />
</application>
有什么想法吗?有什么想法可以尝试,以获取有关该问题的更多信息?
Any ideas? Any ideas on things to try, to get more info about the problem?
推荐答案
正如 Steve H 所说,你不能只使用 file:///data/data/com.droidstogo.boom1/files/parked.jpg 来做这件事.这是您的应用程序私有目录,相机不能在那里写入.例如,您可以使用一些 SD 卡文件 - 它可供所有人使用.
As Steve H said you can't just use file:///data/data/com.droidstogo.boom1/files/parked.jpg for that. It's your application private directory and camera can't write there. You can use some SD card file for example - it's available for all.
正如stealthcopter 所说,intent extra 只是 MediaStore.EXTRA_OUTPUT 没有你的包名.
As stealthcopter said, intent extra is just MediaStore.EXTRA_OUTPUT without your package name.
仅供参考.我猜这个操作实际上不需要你指定的任何权限.
Not an issue just FYI. I guess none of the permissions you specified are actually required for this operation.
这是我的代码示例:
final int REQUEST_FROM_CAMERA=1;
private File getTempFile()
{
//it will return /sdcard/image.tmp
return new File(Environment.getExternalStorageDirectory(), "image.tmp");
}
private void getPhotoClick()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(getTempFile()));
startActivityForResult(intent, REQUEST_FROM_CAMERA);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_FROM_CAMERA && resultCode == RESULT_OK) {
InputStream is=null;
File file=getTempFile();
try {
is=new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//On HTC Hero the requested file will not be created. Because HTC Hero has custom camera
//app implementation and it works another way. It doesn't write to a file but instead
//it writes to media gallery and returns uri in intent. More info can be found here:
//http://stackoverflow.com/questions/1910608/android-actionimagecapture-intent
//http://code.google.com/p/android/issues/detail?id=1480
//So here's the workaround:
if(is==null){
try {
Uri u = data.getData();
is=getContentResolver().openInputStream(u);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//Now "is" stream contains the required photo, you can process it
DoSomeProcessing(is);
//don't forget to remove the temp file when it's not required.
}
}
这篇关于将照片保存到文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!