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

Posted

技术标签:

【中文标题】从我的相机应用程序拍照后,图片未保存到图库【英文标题】:Picture does not save to gallary after taking photo from the my camera application 【发布时间】:2013-05-10 19:30:14 【问题描述】:

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

我的问题是当我从我的相机应用程序中拍摄照片时,它不会将其保存到图库中。当我使用函数 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;
    
 

【问题讨论】:

***.com/questions/15377399/… 和 ***.com/questions/8560501/… 【参考方案1】:
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;
    

   


    

【讨论】:

以上是关于从我的相机应用程序拍照后,图片未保存到图库的主要内容,如果未能解决你的问题,请参考以下文章

Android:如何调用相机[重复]

为啥华为手机拍了照片保存不了怎么回事

Android - 将图像存储到图库

如何将图像保存到图库 Android 应用程序

自定义相机/图库应用程序未准确显示 SD 卡/文件夹

从图库或相机返回后不调用 onActivityResult