无法在传递图像URL的android中的onActivityResult()中获取数据

时间:2018-11-26 08:30:06

标签: android

我想通过Android应用程序将多个图像上传到Firebase。我想获取我在第二个活动(即B活动)到第一个ActivityIA活动中获得的图像的URL。我已经尝试过发布许多答案,但无法解决问题。谁能帮我。这是我的代码

B Activity

      mSelectBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

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

                } });

        }


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


            if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){

                if(data.getClipData() != null){

                     totalItemsSelected = data.getClipData().getItemCount();

                    for(int i = 0; i < totalItemsSelected; i++){

                        Uri fileUri = data.getClipData().getItemAt(i).getUri();

                        String fileName = getFileName(fileUri);

                        fileNameList.add(fileName);
                        fileDoneList.add("uploading");
                        uploadListAdapter.notifyDataSetChanged();

                        StorageReference fileToUpload = mStorage.child("Images").child(fileName);


                        final int finalI = i;
                        fileToUpload.putFile(fileUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                                fileDoneList.remove(finalI);
                                fileDoneList.add(finalI, "done");
    uploadListAdapter.notifyDataSetChanged();
                                Toast.makeText(getApplicationContext(), "Image Uploaded Successfully ", Toast.LENGTH_LONG).show();
      ImageUploadInfo imageUploadInfo = new ImageUploadInfo(taskSnapshot.getDownloadUrl().toString());
                                imageURL=imageUploadInfo.getImageURL();
                                imagesList.add(imageURL);
   Intent idata = new Intent()
                               idata.putExtra("imageURL", imageURL);

                                idata.putExtra("count",totalItemsSelected);

                                setResult(RESULT_OK, idata);
                                finish();

                            }
                        });

                    }

A activity

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       super.onActivityResult(requestCode, resultCode, data);
       switch (resultCode) {
           case 1:
           if (requestCode == 123) {
               if (resultCode == RESULT_OK) {

                   imageURL = data.getStringExtra("imageURL");
                   this.orderItem.setImageURL(imageURL);

               }
               if (resultCode == Activity.RESULT_CANCELED) {
                   //Write your code if there's no result
               }
           }
           break;

           case 2://added24
               if(requestCode==100){//added24
                   if (resultCode == RESULT_OK) {
                       imageURL = data.getStringExtra("imageURL");
                       newString=data.getStringExtra("count");

                   }

               }
               break;
       }

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.order_item_update, menu);


        FrameLayout image_viewCount = (FrameLayout) menu.findItem(R.id.star).getActionView();

        TextView image_count = (TextView) image_viewCount.findViewById(R.id.cart_badge);
        image_count.setText(newString);


        image_viewCount.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(),MultipleActivity.class);
           Order.getInstance().getOrderItems();
                startActivityForResult(intent,100);

            }
        });

我想设置计数(即image_count中的“选择的总数”),但是我无法获取值,因为我没有从Bactivity到A activity获取值

3 个答案:

答案 0 :(得分:0)

您的问题是fileToUpload是异步的,并且您正在循环创建它们;当其中一个任务(第一个任务)结束时,关闭当前活动。您可以模拟一个信号量,以标记每个fileToUpload任务的完成并将结果存储在其中,然后在onSuccess方法中,只需检查所有任务实例是否已完成,然后再关闭当前活动并传递结果(应为数组)顺便说一句)。

答案 1 :(得分:0)

据我了解: Activity-A使用startActivityForResult打开Activity-B。 并且您想要通过onActivityResult()将活动B中的图片网址添加到活动A中。

如果是这样, 您必须在某些操作中将您的活动完成代码添加到Activity-B的onActivityResult()中。 因为将其添加到onActivityResult()意味着您正在等待使用startActivityForResult从Activity-B打开的任何Activity的响应。

答案 2 :(得分:0)

将您的onActivityResult活动中的A更改为

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case 123:
            if (resultCode == RESULT_OK) {
                imageURL = data.getStringExtra("imageURL");
                this.orderItem.setImageURL(imageURL);

            } else if (resultCode == RESULT_CANCELED) {
                //Write your code if there's no result
            }
            break;

        case 100:
            if (resultCode == RESULT_OK) {
                imageURL = data.getStringExtra("imageURL");
                newString = data.getStringExtra("count");
            } else {
                //Write your code if there's no result
            }
            break;
    }
}

您要切换resultCode,使用的大小写是12int的{​​{1}}常数是RESULT_OK

在您的-1活动中,只有有多个MultipleActivity发送回去时,条件data.getClipData() != null才是true。如果只有一个,您可以使用Uri来获得。您可以这样做

getData