Android如何在Arraylist中显示其他活动的意图?

时间:2017-02-19 13:31:29

标签: java android image android-intent arraylist

我有一个MultiplePhotoSelectActivity.java,允许用户选择多张照片并将路径存储在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);
    }
}

这是ArrayList<String> selectedItems来自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;
    }

用户选择照片后,logcat中将显示以下结果

  

D / MultiPhotoSelectActivity:所选项目:[/ storage / emulated/0/Pictures/Screenshot_1486795867.png,/storage/emulated/0/Pictures/15592639_1339693736081458_1539667284_n.jpg,/storage/emulated/0/15592639_1339693736081458_1539667284_n.jpg]

现在的问题是,我想在用户选择图像之后使用数组列表中的文件路径在我的另一个活动中显示图像

这是应该接收意图数据的PreuploadActivity.java。 这是允许用户在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);
        }
    });

这是onActivityResult(),它应该从MultiplePhotoSelectActivity.java

接收Intent数据
 @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();

            }


        }

所以现在onActivityResult() PreuploadActivity.java我无法显示从MultiplePhotoSelectActivity.java发送的ArrayList中的图像。我怀疑putExtra在意图中是错误的,我尝试了远远但仍然没有什么不同。

The answer of this Stackoverflow question

putParcelable or putSerializable like the answer

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

所以我需要知道,

1)我应该如何将putExtra和getExtra放在两个Activity的intent中,以便发送和接收图像的ArrayList?

2)我的手柄是否正确显示图像?如果不是,请告诉我我做错了什么。

编辑:尝试Aslam Hossin解决方案 我试过这个之后

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

我收到以下错误 this warning from android studio

1 个答案:

答案 0 :(得分:0)

在查看了一些文档后,我发现我犯了一些错误

MultiPhotoSelectActivity.java

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

我发现,上面的代码有3个错误,

1)在MultiPhotoSelectActivity.java中不应该是新意图,但应该将数据发送回PreuploadActivity.java

2)我应该像这样setResult

setResult(Activity.RESULT_OK, data);

3)根据以下文档,我在finish()之后添加setResult()

  

只有在您调用finish()后才会返回数据。

之前,您需要在调用finish()之前调用setResult(),否则将返回结果。

我通过在PreuploadActivity.java中设置结果代码来解决此问题,如下所示

 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");

这是MultiPhotoSelectActivity.java我做了以下更改

    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();
    }
}