通过意图打开相机应用程序并将图片保存在SD和imageView上

时间:2012-10-12 06:33:41

标签: android android-intent camera imageview sd-card

  

可能重复:
  Using Camera and storing captured result in SDCard in android

我想从相机应用中获取照片,将其保存在SD上并设置imageView。 我在下面做了一个代码。保存和imageView有时工作。但有时图片保存在SD上,而imageView不起作用。

当imageView不起作用时,似乎mOutUri在onActivityResult中变为null。

我试图在clkbutton中的sharedPreferences上保存mOutUri。我可以在onActivityResult中看到uri但是imageView不起作用。此时,mOutUri也为空。

public void clkbutton(View v){
    Intent intent = new Intent();

    // open camera app
    intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

    // save data in SD card
    Date date = new Date();
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd-kk-mm-ss");
    String newPicFile = df.format(date) + ".jpg";
    mNewPicFile = newPicFile;
    String outPath = "/sdcard/" + newPicFile;
    File outFile = new File(outPath);
    mOutUri = Uri.fromFile(outFile);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, mOutUri);       
    startActivityForResult(intent, REQUEST_CAPTURE_IMAGE);
}


 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    ImageView imageView = (ImageView) findViewById(R.id.image_view);
    imageView.setImageURI(mOutUri);    
 }

奇怪的是,它有时会出错,有时会有效。

2 个答案:

答案 0 :(得分:0)

在setImageUri之前的onActivityResult中,您应该检查文件是否存在。当应用更改方向时,您可能会丢失mOutUri内容,并且当您打开相机时,它会发生。您应该在活动onSaveInstanceState中实现,在那里存储您的uri和onRestoreInstanceState的首选项,在那里恢复您的uri。

答案 1 :(得分:0)

尝试以下代码:

    static Uri capturedImageUri=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
     this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
     photoButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    Calendar cal = Calendar.getInstance();
    File file = new File(Environment.getExternalStorageDirectory(),  (cal.getTimeInMillis()+".jpg"));
    if(!file.exists()){
    try {
    file.createNewFile();
    } catch (IOException e) {
    // TODO Auto-generated catch block
       e.printStackTrace();
     }
       }else{
     file.delete();
     try {
   file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
    e.printStackTrace();
}
}
capturedImageUri = Uri.fromFile(file);
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
startActivityForResult(i, CAMERA_RESULT);
   }
});

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
      if (requestCode == CAMERA_REQUEST) {  
    //Bitmap photo = (Bitmap) data.getExtras().get("data");
    //imageView.setImageBitmap(photo);
    try {
    Bitmap bitmap = MediaStore.Images.Media.getBitmap( getApplicationContext().getContentResolver(),  capturedImageUri);
     imageView.setImageBitmap(bitmap);
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
     e.printStackTrace();
      } catch (IOException e) {
    // TODO Auto-generated catch block
     e.printStackTrace();
        }
     }  
   }

并且也不要忘记在android showsets文件中添加一些权限,如:

 `note: add permission WRITE_EXTERNAL_STORAGE in manifest file.`