2014-10-31 163 views
1

我想從android圖庫中選擇圖像。這是我的代碼。它適用於單幅圖像。但是,如果選擇多個圖像,它給我回空。任何想法怎麼回事錯從圖庫中選擇多個圖像

Button addNewCart = (Button) findViewById(R.id.imageSelect); 
     addNewCart.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(); 
        intent.setType("image/*"); 
        intent.setAction(Intent.ACTION_GET_CONTENT); 
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); 
        startActivityForResult(Intent.createChooser(intent, 
          "select multiple images"), 100); 
      } 
     }); 

這裏是活動

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(resultCode == Activity.RESULT_OK){ 
     String[] all_path = data.getStringArrayExtra("all_path"); 
     if(data != null) 
     { 
      Uri selectedImageUri = data.getData(); 
      System.out.println(selectedImageUri); 

     } 
    } 
} 

任何想法的代碼?

感謝

+0

注:EXTRA_ALLOW_MULTIPLE選項僅在Android的API 18和更高版本中提供。 – Manmohan 2014-10-31 05:55:15

+0

是的,我補充說<使用-SDK 機器人:的minSdkVersion = 「18」 機器人:targetSdkVersion = 「21」/>? – Achayan 2014-10-31 06:00:58

回答

1

嘗試這樣,

private final int PICK_IMAGE_MULTIPLE =1; 

addNewCart.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Intent intent = new Intent(); 
         intent.setType("image/*"); 
         intent.setAction(Intent.ACTION_GET_CONTENT); 
         intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); 
         startActivityForResult(Intent.createChooser(intent, 
           "select multiple images"), PICK_IMAGE_MULTIPLE); 
       } 
      }); 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if(resultCode == Activity.RESULT_OK){ 
    if(requestCode == PICK_IMAGE_MULTIPLE){ 
      String[] imagesPath = data.getStringExtra("data").split("\\|"); 
    } 
     } 
    } 
+0

獲得零指示字例外:( – Achayan 2014-10-31 05:54:03

+0

哪一個得到的NullPointerException – SeanChense 2014-10-31 06:55:31

+0

字符串[] imagesPath = data.getStringExtra( 「數據」)分割( 「\\ |」); – Achayan 2014-10-31 07:58:39