3

我正在从我的应用程序中调用默认图库应用程序来选择任何照片。下面是我从图库中获取所选图像路径的代码。除少数照片外,所有照片都可以正常工作。当我从图库中选择任何 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

4 回答 4

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;
}
于 2012-07-09T13:26:08.947 回答
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();
 }
}
}

或者

请检查以下链接

或者

如何为我的应用从图库(SD 卡)中选择图像?

于 2012-07-09T13:17:55.683 回答
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);
}
于 2015-11-17T15:39:59.423 回答
0

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

经过多次研究,我发现Custom Camera Gallery library已经可以帮助您做到这一点。

于 2015-12-03T09:30:19.550 回答