无法从Android中的图库中选择少量照片

时间:2012-07-09 13:07:39

标签: android cursor gallery uri

我从我的应用程序调用默认图库应用程序来选择任何照片。下面是我从库中获取所选图像路径的代码。它对所有照片都很好,除了很少。当我从图库中选择任何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);
            ......      

4 个答案:

答案 0 :(得分:7)

有时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;
}

答案 1 :(得分: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?

答案 2 :(得分:0)

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);
}

答案 3 :(得分:0)

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

经过多次研究,我发现Custom Camera Gallery library可以帮助你做这件事。