Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  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?

like image 229
Peter vdL Avatar asked Apr 23 '10 04:04

Peter vdL


People also ask

Why is my picture not saving?

This can cause issues like the pictures taken with the camera don't save in Gallery. Though, you can easily fix this by visiting your Android phone's Settings > Apps > Camera and tapping on the “Storage” or “Manage Storage” option. From here, you can just tap on the “Clear Cache” button to get rid of its cache content.

Why can't I save a file?

If you get the error message "Failed to save file." and you are unable to save your file, go to the File menu and select [Save As] to save the file in a different location. If you are still unable to save the file, please check your security software settings.

How do I save a photo to a file?

Control-click the illustration that you want to save as a separate image file, and then click Save as Picture. In the Save as type list, select the file format that you want. In the Save As box, type a new name for the picture, or just accept the suggested file name.

Why won't my images save as JPEG?

From Start> Control Panel (or Start> Settings> Control Panel)> Default Programs> select Windows Photo Viewer> click on Set this program as default. Then, also from Default Programs> Associate a file type> select JPG and select Windows Photo Viewer. Repeat for JPEG and JPE. Was this reply helpful?


2 Answers

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

}
like image 67
Fedor Avatar answered Oct 01 '22 20:10

Fedor


Is it because you've added an extra dot:

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

Instead of:

 intent.putExtra("com.droidstogo.boom1"
like image 37
stealthcopter Avatar answered Oct 01 '22 19:10

stealthcopter