2012-07-09 68 views
3

我在我的應用程序中調用默認圖庫應用程序來選擇任何照片。以下是我從代碼庫中獲取所選圖像路徑的代碼。除少數照片外,所有照片都能正常工作。當我選擇PICASA上傳的任何照片時,應用程序正在強制關閉。請幫幫我。無法從Android中的圖庫中選擇少量照片


內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); 
      ......  
+0

LogCat告訴你什麼? – 2012-07-09 13:08:46

+0

NullpointerException – Santhosh 2012-07-09 13:09:23

+0

它在哪一行給你一個NPE?是data.getData()!= null? – 2012-07-09 13:11:36

回答

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

OR

請檢查下面link

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

+0

謝謝! 「BitmapFactory.decodeStream」似乎適用於我。 – M0CH1R0N 2016-08-01 20:15:57

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

當前的Android系統(第一版 - > 2.3.3 - > 4.4.2連)貌似不能選擇多個文件,所以您需要自定義的畫廊做那。

研究了很多次,我發現Custom Camera Gallery library可以幫助你做那件事。

相關問題