从我的相机应用程序拍摄照片后,图片无法保存到图库

时间:2013-05-10 19:30:14

标签: android performance android-intent android-camera android-camera-intent

我必须从相机应用程序拍摄照片并将其发送给邮件。

我的问题是,当我从相机应用程序中取出照片时,它不会将其保存到图库。当我使用函数getlastimageid(它需要摄像机拍摄的最后一张图像的地址)时,它需要的地址为我的相机应用程序没有拍摄的图像,但是它取得了相机最后拍摄的图像的ID。 注意:在三星手机中它可以正常工作,即照片自动保存到图库但不在其他图片中。

我的代码是

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();
}

}

我用来获取相机拍摄的最后一张照片的imageid的功能是:

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

1 个答案:

答案 0 :(得分:0)

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

   }


    }
}