0

I have a MultiplePhotoSelectActivity.java which let user select multiple photo and store the path in an ArrayList.

 public void btnChoosePhotosClick(View v){

    ArrayList<String> selectedItems = imageAdapter.getCheckedItems();

    if (selectedItems!= null && selectedItems.size() > 0) {
        //Toast.makeText(MultiPhotoSelectActivity.this, "Total photos selected: " + selectedItems.size(), Toast.LENGTH_SHORT).show();
        Log.d(MultiPhotoSelectActivity.class.getSimpleName(), "Selected Items: " + selectedItems.toString());
        Intent intent = new Intent(MultiPhotoSelectActivity.this,PreuploadActivity.class);
        intent.putStringArrayListExtra("selectedItems", selectedItems);
        setResult(RESULT_OK, intent);
        startActivity(intent);
    }
}

This is ArrayList<String> selectedItems come from imageAdapter

ArrayList<String> getCheckedItems() {
        ArrayList<String> mTempArry = new ArrayList<>();

        for(int i=0;i<mImagesList.size();i++) {
            if(mSparseBooleanArray.get(i)) {
                mTempArry.add(mImagesList.get(i));
            }
        }

        return mTempArry;
    }

After user choose the photo,the following result will appear in logcat

D/MultiPhotoSelectActivity: Selected Items: [/storage/emulated/0/Pictures/Screenshot_1486795867.png, /storage/emulated/0/Pictures/15592639_1339693736081458_1539667284_n.jpg, /storage/emulated/0/15592639_1339693736081458_1539667284_n.jpg]

The problem now is,I want to display the image in my another activity using the file path in the array list,after the user choose the image

Here is PreuploadActivity.java that should be receive the intent data. This is the button to let user choose photo in MultiplePhotoSelectActivity.java

//this button will open gallery,and select photo
    addPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(PreuploadActivity.this,MultiPhotoSelectActivity.class);
            startActivityForResult(intent,PICK_IMAGE_REQUEST);
        }
    });

This is the onActivityResult() which should receive the Intent data from MultiplePhotoSelectActivity.java

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data.getData() !=null){

        ArrayList<String> selectedItems = data.getStringArrayListExtra("selectedItems");

        for(String selectedItem : selectedItems){

            Uri filePath = Uri.parse(selectedItem);


            try{
                // bimatp factory
                BitmapFactory.Options options = new BitmapFactory.Options();

                options.inSampleSize = 8;

                bitmap = BitmapFactory.decodeFile(filePath.getPath(),
                        options);


                //Setting image to ImageView
                ImageView imageView = new ImageView(getApplicationContext());
                LinearLayout.LayoutParams layoutParams =
                        new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                ViewGroup.LayoutParams.MATCH_PARENT);
                imageView.setLayoutParams(layoutParams);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imageView.setPadding(0, 0, 0, 10);
                imageView.setAdjustViewBounds(true);
                imageView.setImageBitmap(bitmap);

                linearMain.addView(imageView);

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

            }


        }

So now in onActivityResult() of PreuploadActivity.java I cant display back the image in the ArrayList which sent from MultiplePhotoSelectActivity.java.I suspect is something wrong when putExtra in the intent,what I tried so far but still no different.

The answer of this Stackoverflow question

putParcelable or putSerializable like the answer

How to transfer a Uri image from one activity to another?

So what I need to know,

1) How should I putExtra and getExtra in the intent in both Activity in order send and receive the ArrayList of the image?

2) Is my handle to display the image correct?If no,please tell me what I doing wrong.

EDIT:Try for Aslam Hossin solution After I tried this

ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("selectedItems ");

I got the following error this warning from android studio

Community
  • 1
  • 1
ken
  • 2,426
  • 5
  • 43
  • 98

1 Answers1

0

After looking at some documentation I figure out I make a few mistake

MultiPhotoSelectActivity.java

Intent intent = new Intent(MultiPhotoSelectActivity.this,PreuploadActivity.class);
    intent.putStringArrayListExtra("selectedItems", selectedItems);
    setResult(RESULT_OK, intent);
    startActivity(intent);

I figure out,there are 3 mistake at above code,

1)In MultiPhotoSelectActivity.java should not a new intent,but it should be send the data back to PreuploadActivity.java

2) I should setResult like this

setResult(Activity.RESULT_OK, data);

3) According to the documentation as below ,so I add finish() after setResult()

Data is only returned once you call finish(). You need to call setResult() before calling finish(), otherwise, no result will be returned.

I solve it by setting result code in PreuploadActivity.java like below

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK ){
       //setting Activity.RESULT_OK  


            ArrayList<String> selectedItems = data.getStringArrayListExtra("selectedItems");

This is MultiPhotoSelectActivity.java I do the following changes

    ArrayList<String> selectedItems = imageAdapter.getCheckedItems();

    if (selectedItems!= null && selectedItems.size() > 0) {
        //Toast.makeText(MultiPhotoSelectActivity.this, "Total photos selected: " + selectedItems.size(), Toast.LENGTH_SHORT).show();
        Log.d(MultiPhotoSelectActivity.class.getSimpleName(), "Selected Items: " + selectedItems.toString());
        final Intent data = new Intent();
        data.putStringArrayListExtra("selectedItems", selectedItems);
        setResult(Activity.RESULT_OK, data);
        finish();
    }
}
Graham
  • 7,431
  • 18
  • 59
  • 84
ken
  • 2,426
  • 5
  • 43
  • 98