保存照片文件的问题文件、照片、问题

2023-09-12 11:13:08 作者:不朽少年

人,我的还是的不能保存的图像时,我送一个意图,要求相片才能作出。下面是我在做什么:

请一个URI重新presenting的路径名

  android.content.Context C = getApplicationContext();

字符串FNAME = c.getFilesDir()getAbsolutePath()+/ parked.jpg。

java.io.File的文件=新的java.io.File(FNAME);

乌里=了fileURI Uri.fromFile(文件);
 

创建的意图(不要忘了PKG名字!),然后启动活性

 私有静态诠释TAKE_PICTURE = 22;

意向意图=新的意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);

intent.putExtra(com.droidstogo.boom1。+ MediaStore.EXTRA_OUTPUT,了fileURI);
startActivityForResult(意向,TAKE_PICTURE);
 
word 保存文件出故障 保存后打开,里头的图片全没了

相机活动开始了,我可以拍张照片,并批准它。我的 onActivityResult()然后被调用。但我的文件不会被写入。 URI是:文件:///data/data/com.droidstogo.boom1/files/parked.jpg

我可以(通过不把多余的进入意向)创建缩略图确定,可以编写一个文件就OK了,后来又读回)。

任何人都可以看到简单的错误我在做什么?在logcat中没有明显的显示出来 - 相机显然是拍摄照片。谢谢你,

彼得·

我要指出,我在AndroidManifest.xml文件中设置相应的权限:

 <使用-权限的Andr​​oid:名称=android.permission.READ_OWNER_DATA/>
    <使用-权限的Andr​​oid:名称=android.permission.WRITE_OWNER_DATA/>

    <使用-权限的Andr​​oid:名称=android.permission.CAMERA/>

    <使用特征的android:NAME =android.hardware.camera/>
    <使用库机器人:名称=com.google.android.maps/>



< /用途>
 

任何想法?对事物的任何想法去尝试,以获取有关问题的更多信息?

解决方案

正如史蒂夫ħ说你不能只使用文件:///data/data/com.droidstogo.boom1/files/parked.jpg了点。这是你的应用程序私有目录和相机不能写在那里。你可以使用一些SD卡的文件 - 例如,它适用于所有

由于stealthcopter说,额外的意图只是MediaStore.EXTRA_OUTPUT没有你的包名。

不是一个问题,仅供参考。我猜你没有指定的实际需要进行此操作的权限。

下面是我的code样品:

 最终诠释REQUEST_FROM_CAMERA = 1;

私人文件getTempFile()
{
    //它将返回/sdcard/image.tmp
    返回新的文件(Environment.getExternalStorageDirectory(),image.tmp);
}

私人无效getPhotoClick()
{
  意向意图=新的意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
  intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(getTempFile()));
  startActivityForResult(意向,REQUEST_FROM_CAMERA);
}


保护无效onActivityResult(INT申请code,INT结果code,意图数据){
  如果(要求code == REQUEST_FROM_CAMERA和放大器;&安培;结果code == RESULT_OK){
    InputStream的是= NULL;

    文件fil​​e = getTempFile();
    尝试 {
        是=新的FileInputStream(文件);
    }赶上(FileNotFoundException异常E){
        e.printStackTrace();
    }

    //在HTC Hero的所需的文件将不会被创建。由于HTC Hero的定制摄像头
    //应用程序执行和它的作品的另一种方式。它不写入一个文件,而是
    //它写入到媒体库,并返回URI的意图。更多信息可以在这里找到:
    //http://stackoverflow.com/questions/1910608/android-actionimagecapture-intent
    //http://$c$c.google.com/p/android/issues/detail?id=1480
    //所以这里的解决方法:
    如果(是== NULL){
        尝试 {
            乌里U = data.getData();
            是= getContentResolver()openInputStream(U)。
        }赶上(FileNotFoundException异常E){
            e.printStackTrace();
        }
    }

    //现在是流包含所需的照片,你可以处理它
    DoSomeProcessing(是);

    //不要忘了删除临时文件时,它不是必需的。
  }

}
 

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

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?

解决方案

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:

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

}