18

谷歌提供了这个通过意图拍照的通用代码:

@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看似与照片数据一起运行,并使后续操作认为意图数据为空。

看来这是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

4 回答 4

18

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

在此处查看另一个已解决的相同问题:Android 相机:数据意图返回 null

于 2013-08-24T16:49:23.497 回答
1

基本上,有两种方法可以从相机中检索图像,如果您使用通过意图附加功能发送捕获的图像,则无法在 intent.getData() 上的 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");              
            }           
        } 
    }
}
于 2015-10-16T14:42:08.573 回答
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;
}

编辑:

将这些添加到清单:

开始 Api 19 及更高版本 READ_EXTERNAL_STORAGE 应显式声明

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

向你显现。

于 2013-08-24T17:37:12.577 回答
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()然后通过编写完成来解决,android.provider.MediaStore.EXTRA_OUTPUT这解决了我的错误。

于 2015-11-28T16:52:52.973 回答