我在onActivityResult上获得了空数据

时间:2014-04-08 12:24:07

标签: android android-intent onactivityresult

我得到了这个代码(来自android web),一旦启动活动,就会存储一个文件位置来存储图像

{
    public static final int MEDIA_TYPE_IMAGE = 1;   
    public static final int MEDIA_TYPE_VIDEO = 2;

    /** 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 */   private static
    File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                      Environment.DIRECTORY_PICTURES), "MyCameraApp");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
       if (! mediaStorageDir.mkdirs()){
          Log.d("MyCameraApp", "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 if(type == MEDIA_TYPE_VIDEO) {
       mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_"+ timeStamp + ".mp4");
    } else {
       return null;
    }

    return mediaFile;   
}

然后我开始这样的活动:

 cameraButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {           
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);            
        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);            
   }        
 });

然后当我得到结果时:

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

我看到调试它,“data”为空..

我做了同样的事情,但没有添加

intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

当我添加没有额外的我收到数据信息..

猜我在写/读时有问题?但怎么样?我从android网页上复制!!

我有permision相机写作阅读外部存储!

帮助!!!!!!!!!!!!!!!

1 个答案:

答案 0 :(得分:2)

当您为意图指定 MediaStore.EXTRA_OUTPUT 标记时,拍摄的照片将存储在您的手机存储中,并且不会返回任何数据。

来自Docs

  

调用者可以传递额外的EXTRA_OUTPUT来控制该图像的写入位置。如果EXTRA_OUTPUT不存在,则在额外字段中返回小尺寸图像作为Bitmap对象。这对于只需要小图像的应用程序非常有用。如果存在EXTRA_OUTPUT,则将将全尺寸图像写入EXTRA_OUTPUT的Uri值。

当您使用该方法存储拍摄的照片时,您必须将URI与意图一起传递,以告知存储拍摄照片的位置

NightCrawler提供了有关如何获取数据的链接 onActivityResult

复制最重要的部分:

YourActivity extends Activity{
  onCreate{
     Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(cameraIntent, CAMERA_REQUEST);
  }

  onActivityResult(parameters..) {  
     if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        yourImageView.setImageBitmap(photo);
     }  
  } 
}