tags:

views:

95

answers:

1

Hi I'm inserting an image from the camera (Taking a picture) into the MediaStore.Images.Media datastore.

Does anyone know how I can go about displaying the last picture taken?

I used Uri image = ContentUris.withAppendedId(externalContentUri, 45); to display an image from the datastore but obviously 45 is not the correct image.

I try to pass the information from the previous activity (Camera) to the display activity but I'm assuming due to the photo call back being its own thread the value never gets set. Photo code is as follows

Camera.PictureCallback photoCallback = new Camera.PictureCallback() {

    public void onPictureTaken(byte[] data, Camera camera) {
        // TODO Auto-generated method stub
        FileOutputStream fos;
        try
        {
            Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
            fileUrl = MediaStore.Images.Media.insertImage(getContentResolver(),  bm, "LastTaken", "Picture");

            if(fileUrl == null)
            {
                Log.d("Still", "Image Insert Failed");
                return;
            } else
            {

                 picUri = Uri.parse(fileUrl);
                sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, picUri));
            }
        }
        catch(Exception e)
        {
            Log.d("Picture", "Error Picture: ", e);
        }
        camera.startPreview();

    }
};
A: 

You may use something like this to obtain the last image taken

    final ContentResolver cr = getContentResolver();
    final String[] p1 = new String[] {
            MediaStore.Images.ImageColumns._ID,
            MediaStore.Images.ImageColumns.DATE_TAKEN
    };
    Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null,
            p1[1] + " DESC");

    if ( c1.moveToFirst() ) {
        Log.d(TAG, "last picture (" + c1.getInt(0) + ") taken on: " +
                          new Date(c1.getLong(1));
    }

    c1.close();
dtmilano