java – How to pass an image from one activity to another activity in this app?-ThrowExceptions

Exception or error:

I am new to Android Studio and need your help. There are similar questions, but I read them but could not do this.

There is app, let’s say APP2. Users can select an image using FloatingActionButton. Then, the image is segmented.
These code parts are from the MainActivity.java file of the APP2;

private FloatingActionButton mFabPickImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    setupViews();
}

private void setupViews() {
    mFabPickImage = findViewById(R.id.fab_pick_image);

    if (mFabPickImage != null) {
         mFabPickImage.setOnClickListener(new View.OnClickListener() {
            @Override
             public void onClick(View v) {
                Intent intent;
                if (Build.VERSION.SDK_INT >= 19) {
                    intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false);
                    intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
                } else {
                    intent = new Intent(Intent.ACTION_GET_CONTENT);
                }

                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setType("image/*");

                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

                ActivityLauncher.launchActivityForResult(MainActivity.this,
                        Intent.createChooser(intent, getString(R.string.app_name)),
                        REQUEST_PICK_IMAGE);
            }

        });
    }
}

I have an app, let’s say APP1. I can open APP2 from APP1 by the btnSegmentation?.setOnClickListener function in app1_Activity.kt by;

        btnSegmentation?.setOnClickListener{
             val intent = Intent()
               intent.setClassName("com.example.APP2", "com.example.APP2.MainActivity")
              startActivity(intent)
              finish()
        }

But I dont want to allow users to select an image in APP2. Because users have already chosen an image in app1_Activity.kt with the following codes;

        iv_crop.setImageURI(result.uri)
        val resultUri = result.uri

        var drawable:BitmapDrawable = iv_crop.getDrawable() as BitmapDrawable
        var bitmapImg = drawable.getBitmap()
        bitmapImg = Bitmap.createScaledBitmap(bitmapImg, INPUT_SIZE, INPUT_SIZE, false)

So, app1_Activity.kt should send this bitmapImg to the MainActivity.java in APP2 for segmentation.

I added these codes into the MainActivity.java in APP2 to receive url of the image;

`Bundle bd = getIntent().getExtras();

    Uri uri = bd.getParcelable("uri");
    Log.e("URI", uri.toString());
    try {
        Bitmap ReceivedBitmap = Media.getBitmap(this.getContentResolver(), uri); 
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }`

Now, I want to remove mFabPickImage and use the image in “ReceivedBitmap” for segmentation. But, how to do this? How to modify the setupViews() function for this purpose?

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *