2016-11-05 65 views
0

我嘗試從照片庫中選擇多個圖像並將其保存在我的sdCard上的自定義文件夾中。 我寫了一些代碼,但我有nullPintException 這是我的源使用Intents從Android上的照片庫中選擇多個圖像NullPointerException

Intent intent = new Intent(); 
    intent.setType("image/*"); 
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(Intent.createChooser(intent,"Select Picture"), PICK_FROM_GALLERY); 

,這是OnActivityResultCode

if(data!=null) 
      { 
       ClipData clipData = data.getClipData(); 
       for (int i = 0; i < clipData.getItemCount(); i++) 
       { 
        Uri selectedImage = clipData.getItemAt(i).getUri(); 

        if (selectedImage != null) { 
         mCurrentPhotoPath = getRealPathFromURI(selectedImage); 
         BitmapFactory.Options options = new BitmapFactory.Options(); 
         options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
         Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, options); 
         // bitmap=getResizedBitmap(bitmap,1280,720); 
         String partFilename = currentDateFormat(); 
         if (bitmap != null) { 
          SaveImage(bitmap, partFilename); 
         } 
        } 
       } 




public String getRealPathFromURI(Uri uri) { 
    Cursor cursor = CarRecieveActivity.this.getContentResolver().query(uri, null, null, null, null); 
    cursor.moveToFirst(); 
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
    return cursor.getString(idx); 
} 




     private void SaveImage(Bitmap finalBitmap,String fileName) { 


    File myDir = new File(rootDirectory + "/"+vinNumber.getText().toString()); 
    myDir.mkdirs(); 
    String fname = fileName+".jpg"; 
    File file = new File (myDir, fname); 
    try { 
     FileOutputStream out = new FileOutputStream(file); 
     finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 
     out.flush(); 
     out.close(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

這是我的源。我有兩個錯誤。 第一當我點擊只有一個圖像在我的畫廊clipData爲空;第二,當我從圖庫中選擇多張圖像,然後點擊確定按鈕,我有這個錯誤

java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it 

錯誤是在這個函數getRealPathFromURI() 我怎麼能解決這兩個問題嗎? 感謝

從設備
+0

請參考此答案http://stackoverflow.com/a/27115327/7045114 –

+0

如果您只選擇一個圖像,則data.getData()是uri。 – greenapps

+0

不要使用像'getRealPathFromURI()'這樣的函數。你不需要一個真正的路徑。使用getContentResolver()。openInputStream(uri)打開輸入流並將該流用於BitmapFactory。 – greenapps

回答

0

選擇圖像

//Uri to store the image uri 
private List<Uri> filePath; 
public String path[]; 
public static List<ImageBeen> listOfImage; 

//Image request code 
    private int PICK_IMAGE_REQUEST = 1; 
    private int PICK_IMAGE_REQUEST_MULTI = 2; 

    // select multiple image 
    private void pickImages() { 

    filePath = new ArrayList<>(); 
    listOfImage = new ArrayList<>(); 

     Intent intent; 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
       intent = new Intent(); 
       intent.setType("image/*"); 
       intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); 
       intent.setAction(Intent.ACTION_GET_CONTENT); 
       intent.setAction(Intent.ACTION_OPEN_DOCUMENT); 
       intent.addCategory(Intent.CATEGORY_OPENABLE); 
       startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST_MULTI); 
}else { 
      intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      intent.setAction(Intent.ACTION_OPEN_DOCUMENT); 
      intent.addCategory(Intent.CATEGORY_OPENABLE); 
      startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST); 
     } 
} 

活動結果

//handling the image chooser activity result 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (requestCode == PICK_IMAGE_REQUEST_MULTI && resultCode == RESULT_OK && data != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
      ClipData imagesPath = data.getClipData(); 

      if (imagesPath != null) { 
        path = new String[imagesPath.getItemCount()]; 
        for (int i = 0; i < imagesPath.getItemCount(); i++) { 
         filePath.add(imagesPath.getItemAt(i).getUri()); 
         path[i] = getPath(imagesPath.getItemAt(i).getUri()); 
         ImageBeen imageBeen = new ImageBeen(imagesPath.getItemAt(i).getUri()); 
         imageBeen.setPath(path[i]); 
         listOfImage.add(imageBeen); 
         initViewPager(); 
        } 
       }else { 
       path = new String[1]; 
       path[0] = getPath(data.getData()); 
       ImageBeen imageBeen = new ImageBeen(data.getData()); 
       imageBeen.setPath(path[0]); 
       listOfImage.add(imageBeen); 
       initViewPager(); 
      } 

     } else if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null) { 
      path = new String[1]; 
      path[0] = getPath(data.getData()); 
      ImageBeen imageBeen = new ImageBeen(data.getData()); 
      imageBeen.setPath(path[0]); 
      listOfImage.add(imageBeen); 
      initViewPager(); 
     } 

    } 

genarate圖像路徑使用Image URI

//method to get the file path from uri 
    public String getPath(Uri uri) { 

     Cursor cursor = getContentResolver().query(uri, null, null, null, null); 
     String path = null; 
     if(cursor!=null) { 
      if (cursor.moveToFirst()) { 
       String document_id = cursor.getString(0); 
       document_id = document_id.substring(document_id.lastIndexOf(":") + 1); 
       cursor.close(); 

       cursor = getContentResolver().query(
         MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
         null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null); 
       cursor.moveToFirst(); 
       path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); 
       cursor.close(); 

       return path; 
      } 
     } 
     return path; 
    } 

這是圖像豆其中存儲的圖像URI和圖像路徑

public class ImageBeen { 
    private Uri fileUri; 
private String path; 

    public ImageBeen(Uri fileUri) 
    { 
     this.fileUri=fileUri; 
    } 

    public String getPath() { 
     return path; 
    } 


    public void setPath(String path) 
    { 
     this.path=path; 
    } 


    public Uri getFileUri() { 
     return fileUri; 
    } 
} 

對於顯示圖像使用畢加索

編譯 'com.squareup.picasso:畢加索:2.5.2'

Picasso.with(context) 
       .load(imageBeen.getFileUri()) 
       .placeholder(R.drawable.not_vailable) 
       .error(R.drawable.not_vailable) 
       .into(holder.image); 
相關問題