2016-07-11 47 views
0

我正在使用以下代碼從庫中選擇多個文件。但沒有Toast顯示。我在做什麼錯?從庫中選擇後無法檢索多個文件

@SuppressLint("NewApi") public class MainActivity extends Activity { 
     Button clickButton; 
     int PICK_IMAGE_MULTIPLE = 1; 
       String imageEncoded;  
       List<String> imagesEncodedList; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     clickButton= (Button) findViewById(R.id.button1); 
     clickButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       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_IMAGE_MULTIPLE); 
      } 
     }); 
    // setContentView(new myView(this)); 

    } 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    try { 
     // When an Image is picked 
     if (requestCode == PICK_IMAGE_MULTIPLE && resultCode == RESULT_OK 
       && null != data) { 
      // Get the Image from data 

      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
      imagesEncodedList = new ArrayList<String>(); 
      if(data.getData()!=null){ 

       Uri mImageUri=data.getData(); 

       // Get the cursor 
       Cursor cursor = getContentResolver().query(mImageUri, 
         filePathColumn, null, null, null); 
       // Move to first row 
       cursor.moveToFirst(); 

       int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       imageEncoded = cursor.getString(columnIndex); 
       cursor.close(); 

      }else { 
       if (data.getClipData() != null) { 
        ClipData mClipData = data.getClipData(); 
        ArrayList<Uri> mArrayUri = new ArrayList<Uri>(); 
        for (int i = 0; i < mClipData.getItemCount(); i++) { 

         ClipData.Item item = mClipData.getItemAt(i); 
         Uri uri = item.getUri(); 
         mArrayUri.add(uri); 
         // Get the cursor 
         Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null); 
         // Move to first row 
         cursor.moveToFirst(); 

         int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
         imageEncoded = cursor.getString(columnIndex); 
         imagesEncodedList.add(imageEncoded); 
         cursor.close(); 

        } 
        Log.v("LOG_TAG", "Selected Images" + mArrayUri.size()); 
       } 
      } 
     } else { 
      Toast.makeText(this, "You haven't picked Image", 
        Toast.LENGTH_LONG).show(); 
     } 
    } catch (Exception e) { 
     Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG) 
       .show(); 
    } 

    super.onActivityResult(requestCode, resultCode, data); 
    } 
+0

調試你的代碼? – Shubhank

+0

'但沒有吐司顯示.'很高興。那些敬酒說明出了點問題。你想看哪一個? – greenapps

+0

@Shubhank Logcat不顯示任何錯誤消息。 – techno

回答

0

您的代碼按預期工作; 請檢查您的logcat下面的日誌,(我選擇了兩個圖像)。

V/LOG_TAG: Selected Images2