如何从一个嵌套片段中获取图像?

huangapple go评论75阅读模式
英文:

How to get an image from a fragment inside a fragment?

问题

我有一个按以下方式组织的项目:

AdminAddNewMerchantActivity

-> TimeFragment

----> FindFoodAdminFragment

----> MapMerchantFagment

----> PriceFragment

----> StartFragment

----> EndFragment

-> OrdersFragment

----> IncompleteOrders

----> CompleteOrders

PickImageActivity

其中:

-> - 新子目录

----> - 子子目录

我正在尝试选择图像并分配它。我已经成功让它在PickImageActivity中工作,但在尝试从FindFoodAdminFragment中使用相同的代码时遇到问题。我在PickImageActivity中使用的代码如下:

public class PickImageActivity extends AppCompatActivity {
    ImageView image;
    CircleImageView imageBanner;
    private Uri imageUri, imageUri2;
    private static final int GalleryPick = 1, GalleryPickBanner = 2;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick_image);
        .
        .
        .
        image = findViewById(R.id.pi_input_merchant_image);
        imageBanner = findViewById(R.id.pi_input_banner_image);
        Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT, 
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(galleryIntent, GalleryPick);
        .
        .
        .
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if((requestCode == GalleryPick) && (resultCode == RESULT_OK) && (data!=null)) {
            imageUri = data.getData();
            image.setImageURI(imageUri);
            //  Prevalent.currentMerchant.setImage();
        } else if((requestCode == GalleryPickBanner) && (resultCode == RESULT_OK) && (data!=null)) {
            imageUri2 = data.getData();
            imageBanner.setImageURI(imageUri2);
        }
    }
}

问题出现在startActivityForResult(galleryIntent, GalleryPick);。我尝试过super.startActivityForResult(galleryIntent, GalleryPick);getParentFragment().startActivityForResult(galleryIntent, GalleryPick);,但都无法工作。我怎样才能让它从FindFoodAdminFragment中正常工作?

英文:

I have a project organized as follows:

AdminAddNewMerchantActivity

->TimeFragment

---->FindFoodAdminFragment

---->MapMerchantFagment

---->PriceFragment

---->StartFragment

---->EndFragment

->OrdersFragment

---->IncompleteOrders

---->CompleteOrders

PickImageActivity

where:

-> - a new subdirectory

----> - a subsubdirectory

I'm trying to pick an image and assign it. I successfully got it to work in PickImageActivity, but have problems getting the same code to work from FindFoodAdminFragment. The code for PickImageActivity that I use is as follows:

public class PickImageActivity extends AppCompatActivity {
ImageView image;
CircleImageView imageBanner;
private Uri imageUri, imageUri2;
private static final int GalleryPick = 1, GalleryPickBanner = 2;

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick_image);
   .
   .
   .
        image = findViewById(R.id.pi_input_merchant_image);
        imageBanner = findViewById(R.id.pi_input_banner_image);
        Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT, 
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(galleryIntent, GalleryPick);
   .
   .
   .
    }

   public void onActivityResult(int requestCode, int resultCode, Intent data)
   {
           super.onActivityResult(requestCode, resultCode, data);
           if((requestCode == GalleryPick) && (resultCode == RESULT_OK) && (data!=null))
           {
               imageUri = data.getData();
               image.setImageURI(imageUri);
               //  Prevalent.currentMerchant.setImage();
           }
           else if((requestCode == GalleryPickBanner) && (resultCode == RESULT_OK) && (data!=null))
           {
               imageUri2 = data.getData();
               imageBanner.setImageURI(imageUri2);
           }
       }
   }
}

The problem is in startActivityForResult(galleryIntent, GalleryPick); I tried super.startActivityForResult(galleryIntent, GalleryPick); and getParentFragment().startActivityForResult(galleryIntent, GalleryPick); but it doesn't work. How do I get it to work from FindFoodAdminFragment?

答案1

得分: 0

我通过从FindFoodAdminFragment启动新的活动,并使用getIntent().getExtras()和intent.putExtra("myKey", myString)在活动和片段之间传递数据来解决了这个问题。在活动中,startActivityForResult运行良好,所以我在活动中使用了它,然后触发了onActivityResult活动。在onActivityResult中,我putExtra了imageUri,然后我启动了另一个包含FindFoodAminFragment的活动并使用getExtras。感谢大家的帮助。

英文:

I solved it by starting a new activity from FindFoodAdminFragment and using getIntent().getExtras() and intent.putExtra("myKey", myString) to transmit data between the activity and fragment. In an activity startActivityForResult works fine so I used it in the activity then onActivityResult activity is triggered. In onActivityResult i putExtra the imageUri, then I started another activity which contained FindFoodAminFragment and getExtras. Thanks for help all.

huangapple
  • 本文由 发表于 2020年7月26日 21:18:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63100688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定