从库中选择后无法检索多个文件

时间:2016-07-11 07:06:22

标签: android

我正在使用以下代码从图库中选择多个文件。但是没有显示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);
    }

1 个答案:

答案 0 :(得分:0)

您的代码按预期工作; 请检查您的logcat以获取以下日志,(我选择了两张图片)。

V/LOG_TAG: Selected Images2