将照片保存到文件时出现问题

发布于 2024-08-29 14:00:45 字数 1417 浏览 3 评论 0原文

伙计,当我发送请求拍照的意图时,我仍然无法保存照片。这就是我正在做的事情:

  1. 创建一个代表路径名的 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(文件);
    
  2. 创建 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 );
    
  3. 相机活动启动,我可以拍照并批准它。然后我的 onActivityResult() 被调用。但我的文件没有被写入。 URI 为: file:///data/data/com.droidstogo.boom1/files/parked.jpg

  4. 我可以创建缩略图(不将额外内容放入 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:

  1. 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);
    
  2. 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 );
    
  3. 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

  4. I 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小忆控 2024-09-05 14:00:45
  1. 正如 Steve H 所说,您不能仅使用 file:///data/data/com.droidstogo.boom1/files/parked.jpg 来实现此目的。这是您的应用程序私有目录,相机无法在那里写入。例如,您可以使用一些 SD 卡文件 - 它可供所有人使用。

  2. 正如stealthcopter所说,intent extra只是MediaStore.EXTRA_OUTPUT,没有你的包名称。

  3. 这不是问题,仅供参考。我猜想此操作实际上不需要您指定的任何权限。

这是我的代码示例:

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. 
  }

}
  1. 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.

  2. As stealthcopter said, intent extra is just MediaStore.EXTRA_OUTPUT without your package name.

  3. Not an issue just FYI. I guess none of the permissions you specified are actually required for this operation.

Here's my code sample:

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. 
  }

}
棒棒糖 2024-09-05 14:00:45

是不是因为你添加了一个额外的点:

 intent.putExtra("com.droidstogo.boom1."

而不是:

 intent.putExtra("com.droidstogo.boom1"

Is it because you've added an extra dot:

 intent.putExtra("com.droidstogo.boom1."

Instead of:

 intent.putExtra("com.droidstogo.boom1"
七颜 2024-09-05 14:00:45

您的问题可能出在您尝试存储文件的目录上。要将文件保存到 SD 卡,您不需要任何特殊权限,但获取文件夹引用的方式与您执行此操作的方式不同。它还取决于您是否希望以 MediaStore 可以检索的方式保存图像(即图库或相册应用程序,或依赖这些来查找图像的任何其他应用程序)。假设您希望将其列在 MediaStore 中,则执行此操作的代码如下:

ContentValues newImage = new ContentValues(2);
newImage.put(Media.DISPLAY_NAME, "whatever name you want shown");
newImage.put(Media.MIME_TYPE, "image/png");

Uri uri = contentResolver.insert(Media.EXTERNAL_CONTENT_URI, newImage);

try {
    Bitmap bitmap = //get your bitmap from the Camera, however that's done  
    OutputStream out = contentResolver.openOutputStream(uri);
    boolean success = bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
    out.close();
    if (success){
        Log.d("Image Writer", "Image written successfully.");                   
    } else {
        Log.d("Image Writer", "Image write failed, but without an explanation.");
    }

} catch (Exception e){
    Log.d("Image Writer", "Problem with the image. Stacktrace: ", e);
}

在运行 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:

ContentValues newImage = new ContentValues(2);
newImage.put(Media.DISPLAY_NAME, "whatever name you want shown");
newImage.put(Media.MIME_TYPE, "image/png");

Uri uri = contentResolver.insert(Media.EXTERNAL_CONTENT_URI, newImage);

try {
    Bitmap bitmap = //get your bitmap from the Camera, however that's done  
    OutputStream out = contentResolver.openOutputStream(uri);
    boolean success = bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
    out.close();
    if (success){
        Log.d("Image Writer", "Image written successfully.");                   
    } else {
        Log.d("Image Writer", "Image write failed, but without an explanation.");
    }

} catch (Exception e){
    Log.d("Image Writer", "Problem with the image. Stacktrace: ", e);
}

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.)

情绪少女 2024-09-05 14:00:45

正如 Steve 所说,你应该将照片保存在 SD 卡中。您尝试保存的目录是私有的,除非您的设备已root,否则您将无法在其中写入。

String fname = c.getFilesDir().getAbsolutePath()+"/parked.jpg";

尝试用这一行替换这一行

String fname = Environment.getExternalStorageDirectory().getAbsolutePath() + "somePathYouKnownExists" + +"/parked.jpg";

这应该足够了。

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:

String fname = c.getFilesDir().getAbsolutePath()+"/parked.jpg";

with this line

String fname = Environment.getExternalStorageDirectory().getAbsolutePath() + "somePathYouKnownExists" + +"/parked.jpg";

This should be enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文