【问题标题】:Not able to select few photos from Gallery in Android无法从 Android 的图库中选择几张照片
【发布时间】:2015-12-03 17:30:19
【问题描述】:

我正在从我的应用中调用默认图库应用来选择任何照片。下面是我从图库中获取所选图像路径的代码。除少数照片外,所有照片都可以正常工作。当我从图库中选择任何 PICASA 上传的照片时,应用程序会强制关闭。请帮我。


在 onActivityResult()....

            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String selectedPhotoPath = cursor.getString(columnIndex).trim();  <<--- NullPointerException here
            cursor.close(); 
            bitmap = BitmapFactory.decodeFile(selectedPhotoPath);
            ......      

【问题讨论】:

  • 它在哪一行给你一个 NPE?是 data.getData()!=null 吗?
  • columnIndex 为 0,String selectedPhotoPath = cursor.getString(columnIndex).trim();

标签: android cursor gallery uri


【解决方案1】:

有时data.getData(); 返回 null,具体取决于您用于获取图片的应用程序。解决方法是在onActivityResult 中使用上述代码:

/**
*Retrieves the path of the image that was chosen from the intent of getting photos from the galery
*/
Uri selectedImageUri = data.getData();

// OI FILE Manager
String filemanagerstring = selectedImageUri.getPath();

// MEDIA GALLERY
String filename = getImagePath(selectedImageUri);

String chosenPath;

if (filename != null) {

   chosenPath = filename;
} else {

   chosenPath = filemanagerstring;
}

变量chosenPath 将具有所选图像的正确路径。方法getImagePath()是这样的:

public String getImagePath(Uri uri) {
    String selectedImagePath;
    // 1:MEDIA GALLERY --- query from MediaStore.Images.Media.DATA
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if (cursor != null) {
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        selectedImagePath = cursor.getString(column_index);
    } else {
        selectedImagePath = null;
    }

    if (selectedImagePath == null) {
        // 2:OI FILE Manager --- call method: uri.getPath()
        selectedImagePath = uri.getPath();
    }
    return selectedImagePath;
}

【讨论】:

    【解决方案2】:

    请尝试以下代码

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    
    if (resultCode == RESULT_OK){
     Uri targetUri = data.getData();
     textTargetUri.setText(targetUri.toString());
     Bitmap bitmap;
     try {
      bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
      ....
     } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
    }
    

    请查看下方link

    How to pick an image from gallery (SD Card) for my app?

    【讨论】:

    • 谢谢! “BitmapFactory.decodeStream”似乎对我有用。
    【解决方案3】:
    String ImagePath  = "";
    private void setImageFromGallery(Intent data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = this.getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            Log.i("choosepath", "image" + picturePath);
            ImagePath = picturePath;
        } else {
            ImagePath = selectedImage.getPath();   // Add this line 
        }
       ImageView imgView = (ImageView) findViewById(R.id.imgView);
       imgView.setImageBitmap(BitmapFactory.decodeFile(imagePath));
        Bitmap bitmap = Utilities.rotateImage(pictureImagePath);
    }
    

    【讨论】:

      【解决方案4】:

      当前的 Android 系统(第一个版本 -> 2.3.3 -> 甚至 4.4.2)看起来无法选择多个文件,因此您需要自定义库来执行此操作。

      研究了很多遍,我发现Custom Camera Gallery library已经可以帮你做这件事了。

      【讨论】:

        猜你喜欢
        • 2020-10-30
        • 1970-01-01
        • 2020-10-02
        • 2016-07-13
        • 2018-10-04
        • 2013-11-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多