2015-01-26 111 views
0

我在尝试拍照并从onActivityResult()获取它时,在我的android应用程序中遇到了一些问题。我想确保代码可以正常工作,因此我刚刚复制了此页面的确切代码http://developer.android.com/training/camera/photobasics.html拍照android

所以这些是我使用的方法;我叫dispatchTakePictureIntent()时,在button.`

private File createImageFile() throws IOException { 
    // Create an image file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName = "JPEG_" + timeStamp + "_"; 
    File storageDir = Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES); 
    File image = File.createTempFile(
     imageFileName, /* prefix */ 
     ".jpg",   /* suffix */ 
     storageDir  /* directory */ 
    ); 

    // Save a file: path for use with ACTION_VIEW intents 
    mCurrentPhotoPath = "file:" + image.getAbsolutePath(); 
    return image; 
} 

private void dispatchTakePictureIntent() { 
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    // Ensure that there's a camera activity to handle the intent 
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
     // Create the File where the photo should go 
     File photoFile = null; 
     try { 
      photoFile = createImageFile(); 
     } catch (IOException ex) { 

     } 
     // Continue only if the File was successfully created 
     if (photoFile != null) { 
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
        Uri.fromFile(photoFile)); 
      startActivityForResult(takePictureIntent, 15); 
     } 
    } 
}` 

问题的用户点击的是,当onActivityResult()方法被调用时,我得到正确的requestCode,右resultCode为,但第三个参数为空。我想获取图像位图或路径,但是我不能没有它。为什么会发生?是否因为这段代码不应该返回该参数?在那种情况下,我应该如何修改它?

+0

你的路径已经是:如果你想收到预览图像从您的代码删除此行'mCurrentPhotoPath' – njzk2 2015-01-26 18:21:11

+0

是的,我尝试过使用这条道路,但它没有工作,这是因为它被定义为“file:”+ image.getAbsolutePaht();我删除了“file:”,现在它工作 – Mantech 2015-01-26 19:18:26

回答

0

试试此代码测试。

拍摄照片

private void TakePicture(){ 
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
    startActivityForResult(intent, 0); 
} 

保存到文件

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    switch (requestCode) { 
    case 0: 
     if (resultCode == Activity.RESULT_OK) { 

      try{ 

       String filename = "name.jpg"; 
       File sd = Environment.getExternalStorageDirectory(); 
       File dest = new File(sd, filename); 
       Bitmap bp = (Bitmap) data.getExtras().get("data"); 

       FileOutputStream out = new FileOutputStream(dest); 
       bp.compress(Bitmap.CompressFormat.JPEG, 90, out); 
       out.flush(); 
       out.close(); 
       // 

      } catch (Exception e) { 
       Toast.makeText(this, "Failed", Toast.LENGTH_SHORT) 
       .show(); 
       Log.e("Camera", e.toString()); 
      } 
     } 
    } 
} 
+0

这是我以前使用的代码,但后来我发现在某些设备上会导致崩溃。无论如何,我解决了我的问题,我从我已经拥有的变量中获取路径,而没有像例子那样添加“file:”。 – Mantech 2015-01-27 20:13:03

0

缩略图中onActivityResult()只收到,如果你不指定文件的URI拍照后保存图像。这里

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));