伙计,当我发送要求拍照的 Intent 时,我仍然无法保存照片。这是我正在做的:
制作一个代表路径名的URI
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 名称!)并开始 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 );
相机 Activity 开始,我可以拍照,并批准。然后调用我的
onActivityResult()
。但是我的文件没有被写入。 URI 是:file:///data/data/com.droidstogo.boom1/files/parked.jpg我可以创建缩略图 OK(通过不将额外内容放入 Intent),并且可以写入该文件 OK,稍后再读回)。
谁能看出我犯了什么简单的错误? logcat 中没有明显显示 - 相机显然正在拍照。谢谢,
彼得
我应该提到我在 AndroidManifest.xml 文件中设置了适当的权限:
<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>
有什么想法吗?关于要尝试的事情的任何想法,以获取有关该问题的更多信息?
最佳答案
正如 Steve H 所说,您不能只为此使用 file:///data/data/com.droidstogo.boom1/files/parked.jpg。这是你的应用程序私有(private)目录,相机不能在那里写。例如,您可以使用一些 SD 卡文件 - 它可供所有人使用。
正如 stealthcopter 所说,intent extra 只是没有您的包名称的 MediaStore.EXTRA_OUTPUT。
这不是问题,仅供引用。我想您指定的权限实际上都不是此操作所必需的。
这是我的代码示例:
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.
}
}
关于android - 将照片保存到文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2696298/