【问题标题】:No data returned after trying to retrieve and crop an image尝试检索和裁剪图像后未返回数据
【发布时间】:2013-04-22 04:57:21
【问题描述】:

我正在使用图库视图让用户选择图像。当用户选择了一个图像,他将被传递到“裁剪页面”。 (这不是我所做的自定义活动)

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
photoPickerIntent.putExtra("crop", "true"); 
startActivityForResult(photoPickerIntent, 1);

当我返回 onActivityResult 时,我的数据为空,我无法检索裁剪后的图像。

有谁知道如何解决这个问题? 谢谢!

【问题讨论】:

    标签: java android android-intent


    【解决方案1】:

    尝试添加额外的意图

    intent.putExtra("return-data", true);
    

    参见示例:

    http://nullpointerblog.com/2012/06/17/android-photo-chooser-with-crop/

    【讨论】:

    • 为什么只返回 160 x 160 大小的图片?
    • @Sebastian 不知道,这不是裁剪区域的大小吗?
    • 不,不是!不管我选择什么图片或我的裁剪区域有多大!奇怪..
    • @Sebastian 很奇怪,但我想说的不是这个 QA 线程的问题
    【解决方案2】:

    使用此代码

    private static final String TEMP_PHOTO_FILE = "temporary_holder.jpg";  
    
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    photoPickerIntent.setType("image/*");
    photoPickerIntent.putExtra("crop", "true");
    photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
    photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
    startActivityForResult(photoPickerIntent, REQ_CODE_PICK_IMAGE);
    
    private Uri getTempUri() {
       return Uri.fromFile(getTempFile());
    }
    
    private File getTempFile() {
    
      if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
    
        File file = new File(Environment.getExternalStorageDirectory(),TEMP_PHOTO_FILE);
        try {
            file.createNewFile();
        } catch (IOException e) {}
    
        return file;
     } else {
    
        return null;
      }
    }
    
    protected void onActivityResult(int requestCode, int resultCode,
        Intent imageReturnedIntent) {
    
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    
    switch (requestCode) {
        case REQ_CODE_PICK_IMAGE:
            if (resultCode == RESULT_OK) {  
                if (imageReturnedIntent!=null) {
    
                    File tempFile = getTempFile();
    
                    String filePath= Environment.getExternalStorageDirectory()
                        +"/"+TEMP_PHOTO_FILE;
                    System.out.println("path "+filePath);
    
    
                    Bitmap selectedImage =  BitmapFactory.decodeFile(filePath);
                    _image = (ImageView) findViewById(R.id.image);
                    _image.setImageBitmap(selectedImage );
    
                    if (tempFile.exists()) tempFile.delete();
                }
            }
       }       
    }
    

    添加权限

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

    Source

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-06
      • 2012-10-31
      • 1970-01-01
      • 2014-07-06
      • 2018-06-18
      相关资源
      最近更新 更多