views:

5210

answers:

5

For my application, I'd been using my own Camera class for taking images and my own database but soon enough I couldn't really keep up with changes and I decided to use the built in camera application in Android to do the job, but I can't seem to get it to save file. What am I missing here? The application seems to save the file but it's just 0 bytes. I looked up the source code of the Camera application and it's looking for the "output" in Extras to save the file. Any help would be greatly appreciated.

Public class CameraTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button cameraButton = (Button) findViewById(R.id.cameraButton);
        cameraButton.setOnClickListener( new OnClickListener(){
            public void onClick(View v ){
             ContentValues values = new ContentValues();
             values.put(Images.Media.TITLE, "title");
             values.put(Images.Media.BUCKET_ID, "test");
             values.put(Images.Media.DESCRIPTION, "test Image taken");
             Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
             Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
             intent.putExtra("output", uri.getPath());
             startActivityForResult(intent,0);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     if (requestCode== 0 && resultCode == Activity.RESULT_OK){
      ((ImageView)findViewById(R.id.pictureView)).setImageURI(data.getData());
     }
    }


}
+5  A: 

This worked with the following code, granted I was being a little dumb with the last one. I still think there's got to be a better way so that the original image is still saved somewhere. It still sends me the smaller 25% size image.

public class CameraTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button cameraButton = (Button) findViewById(R.id.cameraButton);
    cameraButton.setOnClickListener( new OnClickListener(){
        public void onClick(View v ){

         Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

         startActivityForResult(intent,0);
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (requestCode== 0 && resultCode == Activity.RESULT_OK){
  Bitmap x = (Bitmap) data.getExtras().get("data");
  ((ImageView)findViewById(R.id.pictureView)).setImageBitmap(x);
  ContentValues values = new ContentValues();
     values.put(Images.Media.TITLE, "title");
     values.put(Images.Media.BUCKET_ID, "test");
     values.put(Images.Media.DESCRIPTION, "test Image taken");
     values.put(Images.Media.MIME_TYPE, "image/jpeg");
     Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
     OutputStream outstream;
  try {
   outstream = getContentResolver().openOutputStream(uri);

     x.compress(Bitmap.CompressFormat.JPEG, 70, outstream);
     outstream.close();
  } catch (FileNotFoundException e) {
   //
  }catch (IOException e){
   //
  }
 }
}


}

Also, I do know that the cupcake release of Android should fix the small image size soon.

pvsnp
A: 

thx for the solution. any idea why i always get a nullpointerexception when trying to call the "data" from the result? request and result_code are ok, but the data is empty?

+2  A: 

In your first attempt, it could be that the Camera app crashes when it reads your "output" extra, since it expects it to be a Uri, while you provide a String. The Camera app seems to read the extra flag after capturing the photo. You should just provide uri, and not uri.getPath(). Then, since you already know the URI to the photo, it will not be returned in the onResult call. You need to remember the URI in a member variable.

In the second attempt you will get a scaled down (50%) bitmap back. It is primarily intended for views. I think the full sized bitmap is too large for the memory budget of the application. This may be the reason for the downscale.

Makes sense. I remember getting buffer overflow type problems with bigger images or with more than one unscaled image on the screen...
pvsnp
A: 

I got the same Problem with the Emulator, I tried it on a real Phone an it worked, maybe it is because the Virtual Phone can't really take Picures.

Rene
+1  A: 

FYI , found this on docs : The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.

located here :http://developer.android.com/reference/android/provider/MediaStore.html

so the app can save full size image for you , if you tell it where.

**Edit : This is not the case with HTC devices. HTC (not nexus) that uses htc sense ui have branched from android 1.5 and carry a bug that always save the image in low res. you can lunch activity for camera and use the share function from camera to use the full sized image.

nabulaer