Question

I have to take a photo from camera application and send it to a mail.

My problem is when i take the photo from my camera application it does not save it to gallery .And when i use the function getlastimageid(it takes the address of the last image taken by the camera) it takes the address of the image not taken by my camera application but it takes the id of the image last taken by the camera. note: in samsung mobile it works correctly i.e photo saved to gallery automatically but not in other.

my code is

public class CameraProject extends Activity {

Button button;
Bitmap btm;
Bitmap thumbnail;
String filePath;
  File pic;
final static int CameraData = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera_project);
    button = (Button) findViewById(R.id.cont);


}


//start camera activity
public void camera(View v) {
    Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(i, CameraData);
            }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        btm = (Bitmap) extras.get("data");

    }
        if(resultCode == RESULT_OK)
        {
            //it moves to another activity
            Intent intent1=new Intent (CameraProject.this,INF.class);

            startActivity(intent1);


}
        else {
            //if discard it reopen camera activity
            Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, CameraData);
        }

}

    public void exit(View v)
    {
    finish();
}

}

And the function i used to get the imageid of the last photo taken by the camera is:

private int getLastImageId(){
    final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
    final String imageOrderBy = MediaStore.Images.Media._ID+" DESC";
    Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy);
    if(imageCursor.moveToFirst()){
        int id = imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID));
         fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
        Log.d(TAG, "getLastImageId::id " + id);
        Log.d(TAG, "getLastImageId::path " + fullPath);
       imageCursor.close();
        return id;
    }else{
        return 0;
    }
} 
Was it helpful?

Solution

private void takeNewPicture() {

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    ContentValues values = new ContentValues(3);

    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
    cameraImagePath = getContentResolver().insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraImagePath);
    startActivityForResult(takePictureIntent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 

    String[] filePathColumn = { MediaStore.Images.Media.DATA };
    Uri imageFilePathUri = null;
    if (cameraImagePath != null) {
        Cursor imageCursor = getContentResolver().query(
            cameraImagePath, filePathColumn, null, null, null);
    if (imageCursor != null && imageCursor.moveToFirst()) {
        int columnIndex = imageCursor.getColumnIndex(filePathColumn[0]);
        String filePath = imageCursor.getString(columnIndex);
        imageCursor.close();
        imageFilePathUri = filePath != null ? Uri
                .parse(filePath) : null;
    }

   }


    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top