将照片保存到文件时出现问题
伙计,当我发送请求拍照的意图时,我仍然无法保存照片。这就是我正在做的事情:
创建一个代表路径名的 URI
android.content.Context c = getApplicationContext(); String fname = c.getFilesDir().getAbsolutePath()+"/parked.jpg"; java.io.File 文件 = new java.io.File( fname ); Uri fileUri = Uri.fromFile(文件);
创建 Intent(不要忘记 pkg 名称!)并启动活动
private static int TAKE_PICTURE = 22; 意图意图 = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE ); Intent.putExtra("com.droidstogo.boom1." + MediaStore.EXTRA_OUTPUT, fileUri); startActivityForResult( 意图, TAKE_PICTURE );
相机活动启动,我可以拍照并批准它。然后我的
onActivityResult()
被调用。但我的文件没有被写入。 URI 为: file:///data/data/com.droidstogo.boom1/files/parked.jpg我可以创建缩略图(不将额外内容放入 Intent),并且可以写入该文件,然后再读回)。
谁能看出我犯了什么简单的错误? 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>
有什么想法吗?有什么想法可以尝试,以获得有关问题的更多信息吗?
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:
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);
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 );
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.jpgI can create thumbnail OK (by not putting the extra into the Intent), and can write that file OK, and later read it back).
Can anyone see what simple mistake I am making? Nothing obvious shows up in the logcat - the camera is clearly taking the picture. Thanks,
Peter
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如 Steve H 所说,您不能仅使用 file:///data/data/com.droidstogo.boom1/files/parked.jpg 来实现此目的。这是您的应用程序私有目录,相机无法在那里写入。例如,您可以使用一些 SD 卡文件 - 它可供所有人使用。
正如stealthcopter所说,intent extra只是MediaStore.EXTRA_OUTPUT,没有你的包名称。
这不是问题,仅供参考。我猜想此操作实际上不需要您指定的任何权限。
这
这是我的代码示例:
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.
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.
Here's my code sample:
是不是因为你添加了一个额外的点:
而不是:
Is it because you've added an extra dot:
Instead of:
您的问题可能出在您尝试存储文件的目录上。要将文件保存到 SD 卡,您不需要任何特殊权限,但获取文件夹引用的方式与您执行此操作的方式不同。它还取决于您是否希望以 MediaStore 可以检索的方式保存图像(即图库或相册应用程序,或依赖这些来查找图像的任何其他应用程序)。假设您希望将其列在 MediaStore 中,则执行此操作的代码如下:
在运行 v1.5 的模拟器上,成功将位图保存到 SD 卡上的 DCIM/Camera 文件夹中,其文件名是当前时间。 (自 1970 年 1 月 1 日起,时间以毫秒为单位保存,由于某种原因也称为“纪元”。)
Your problem might be with the directory you're trying to store the file in. To save files to the SD card you don't need any special permissions, but the way you get the folder reference is different to how you've done it. It also depends on whether you want to save the image in a way that can be retrieved by the MediaStore (i.e. things like the gallery or albums application, or any other app that relies on those to find images) or not. Assuming you want it to be listed in the MediaStore, here's the code to do that:
On my emulator running v1.5, that's successfully saves a bitmap onto the SD card in the DCIM/Camera folder with its file name being current time. (The time is saved in milliseconds since 1st Jan 1970, also known as the "Epoch" for some reason.)
正如 Steve 所说,你应该将照片保存在 SD 卡中。您尝试保存的目录是私有的,除非您的设备已root,否则您将无法在其中写入。
:
尝试用这一行替换这一行
这应该足够了。
As Steve said, you should save your picture in your SD card. The directory where you are trying to save is private and unless you have your device rooted you will can't write there.
Try replace this line:
with this line
This should be enough.