Android:将图像保存到图库时解决问题

时间:2013-10-22 15:39:30

标签: android imageview

我设法以编程方式从相机拍摄照片,然后将其显示在imageview上,然后在按下按钮后将其保存在图库中。 它有效,但问题是保存的照片分辨率低......为什么?!

我用这段代码拍了照片:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 

然后我使用以下方法将照片保存在var上

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == CAMERA_PIC_REQUEST) {  
            thumbnail = (Bitmap) data.getExtras().get("data"); 
        }  
    } 

然后在imageview上显示后,我用这个功能将它保存在图库中:

public void SavePicToGallery(Bitmap picToSave, File savePath){


String JPEG_FILE_PREFIX= "PIC";
String JPEG_FILE_SUFFIX= ".JPG";


String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
File filePath = null;
try {
    filePath = File.createTempFile(imageFileName, JPEG_FILE_SUFFIX, savePath);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
FileOutputStream out = null;
try {
    out = new FileOutputStream(filePath);
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
picToSave.compress(CompressFormat.JPEG, 100, out);
try {
    out.flush();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
try {
    out.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

//Add the pic to Android Gallery
String mCurrentPhotoPath = filePath.getAbsolutePath();

MediaScannerConnection.scanFile(this,
        new String[] { mCurrentPhotoPath }, null,
        new MediaScannerConnection.OnScanCompletedListener() {
        public void onScanCompleted(String path, Uri uri) {

        }
});


}

我真的无法弄清楚为什么它会在保存的同时失去如此多的质量。 有什么帮助吗?感谢..

2 个答案:

答案 0 :(得分:0)

您在ImageView中显示的照片是缩略图:

data.getExtras().get("data");

您是否正在调用该方法:

public void SavePicToGallery(Bitmap picToSave, File savePath)

带缩略图?

在这里,您可以按照所有步骤进行操作: http://developer.android.com/training/camera/photobasics.html

答案 1 :(得分:0)

data.getExtras()。get(“data”)只获取缩略图。

要正确完成,您必须声明全局uri变量

 Uri imageCapturedUri ;


 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            imageCaptureUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageCaptureUri);
 startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
     if (requestCode == CAMERA_PIC_REQUEST) {  

          // mImageCaptureUri is your Uri
      }  
  }