MediaStore.EXTRA_OUTPUT呈现数据为空,其他方式保存照片?

时间:2013-05-02 22:25:37

标签: java android android-intent camera

Google提供这种多功能代码,用于通过意图拍摄照片:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

问题在于,如果你像我一样想要将照片作为附加内容传递,那么使用EXTRA_OUTPUT似乎会使用照片数据运行并使后续操作认为目标数据为空。

It appears这是Android的一个大错误。

我正在尝试拍摄照片,然后在新视图中将其显示为缩略图。我想将它保存为用户图库中的全尺寸图像。有没有人知道如何在不使用EXTRA_OUTPUT的情况下指定图像位置?

这是我目前所拥有的:

public void takePhoto(View view) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
//  takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
}

/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
      return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
@SuppressLint("SimpleDateFormat")
private static File getOutputMediaFile(int type){

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "JoshuaTree");

    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("JoshuaTree", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            handleSmallCameraPhoto(data);
        }
    }
}


private void handleSmallCameraPhoto(Intent intent) {
    Bundle extras = intent.getExtras();
    mImageBitmap = (Bitmap) extras.get("data");
    Intent displayIntent = new Intent(this, DisplayPhotoActivity.class);
    displayIntent.putExtra("BitmapImage", mImageBitmap);
    startActivity(displayIntent);
}

}

4 个答案:

答案 0 :(得分:16)

如果您指定了MediaStore.EXTRA_OUTPUT,则所拍摄的图像将被写入该路径,并且不会向onActivityResult提供任何数据。您可以从您指定的内容中读取图像。

在此处查看另一个解决过的相同问题:Android Camera : data intent returns null

答案 1 :(得分:0)

以下是如何实现您想要的目标:

public void onClick(View v) 
  {

    switch(v.getId())

          {

           case R.id.iBCamera:


            File image = new File(appFolderCheckandCreate(), "img" + getTimeStamp() + ".jpg");
            Uri uriSavedImage = Uri.fromFile(image);

            Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            i.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
            i.putExtra("return-data", true);
            startActivityForResult(i, CAMERA_RESULT);

            break;

           }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{

    super.onActivityResult(requestCode, resultCode, data);

    switch(requestCode)

          {

          case CAMERA_RESULT:

                       if(resultCode==RESULT_OK)
                          {
                           handleSmallCameraPhoto(uriSavedImage);     
                          }
                     break;

          }

 }

     private void handleSmallCameraPhoto(Uri uri) 
        {
           Bitmap bmp=null;

              try {
                bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
                   } 
                   catch (FileNotFoundException e) 
                   {

                 e.printStackTrace();
                }


               Intent displayIntent = new Intent(this, DisplayPhotoActivity.class);
             displayIntent.putExtra("BitmapImage", bmp);
            startActivity(displayIntent);


       }

private String appFolderCheckandCreate(){

    String appFolderPath="";
    File externalStorage = Environment.getExternalStorageDirectory();

    if (externalStorage.canWrite()) 
    {
        appFolderPath = externalStorage.getAbsolutePath() + "/MyApp";
        File dir = new File(appFolderPath);

        if (!dir.exists()) 
        {
              dir.mkdirs();
        }

    }
    else
    {
      showToast("  Storage media not found or is full ! ");
    }

    return appFolderPath;
}



 private String getTimeStamp() {

    final long timestamp = new Date().getTime();

    final Calendar cal = Calendar.getInstance();
                   cal.setTimeInMillis(timestamp);

    final String timeString = new SimpleDateFormat("HH_mm_ss_SSS").format(cal.getTime());


    return timeString;
}

修改

将这些添加到Manifest:

应明确声明启动Api 19及以上的READ_EXTERNAL_STORAGE

     *<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />*
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

你明白了。

答案 2 :(得分:0)

基本上,有两种方法可以从Camera中检索图像,如果您使用通过intent extras发送捕获的图像,则无法在intent.getData()&#39;上的ActivityResult上检索它。因为它通过附加内容保存图像数据。

所以这个想法是:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

在ActivityResults上检索它,检查检索到的意图:

    @Override  
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {        
    if (resultCode == RESULT_OK) {            
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {                
            if (intent.getData() != null) {                    
                ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver().openFileDescriptor(intent.getData(), "r");    
                      
                FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();                      
                Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);                     
                parcelFileDescriptor.close();
            } else {                    
                Bitmap imageRetrieved = (Bitmap) intent.getExtras().get("data");              
            }           
        } 
    }
}

答案 3 :(得分:0)

试试这个android.provider.MediaStore.EXTRA_OUTPUT希望能解决你的错误。以下代码适用于我:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, fileUri); 
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

我遇到了同样的问题,每次在使用getIntent().getData()检索其他活动的数据时获取null,然后通过写入完整android.provider.MediaStore.EXTRA_OUTPUT解决这个问题解决了我的错误。