1

I have tried everything and looked at numerous stackoverflow posts and I can't seem to find a solution for this.

I am making a meme generator where on the main activity I can press a button called select image. This will take me to another activity where I am presented with many pictures that I can choose from. I want to be able to click on an image and then have it return me to the main activity and show up on the imageview where the meme is suppose to be.

This is my code for the main activity:

public void openImageLibrary() {
    // ImageLibrary is the class where I code the stuff for the other 
    // activity
    Intent intent = new Intent(this, ImageLibrary.class);
    startActivityForResult(intent, 0);
}

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

    // result for when select is pressed and user has chosen empty meme
    if(requestCode == 0 && resultCode == RESULT_OK && data != null) {
        Bitmap meme = (Bitmap) getIntent().getParcelableExtra("image");


        // Below, memeImage is the ImageView for the main activity
        ImageView imageView = (ImageView) findViewById(R.id.memeImage);
        imageView.setImageBitmap(meme);
    }
}

And this is my code for the other activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_library);
}

public void selectImage(View view) {
    Intent intent = new Intent();


    view.setDrawingCacheEnabled(true);


    view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

    view.buildDrawingCache(true);
    Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false); // clear drawing cache



    intent.putExtra("image", bitmap);
    setResult(RESULT_OK, intent);
    finish();
}
Benjith Mathew
  • 1,211
  • 1
  • 11
  • 23
Zelda
  • 47
  • 1
  • 10

1 Answers1

2

Try the below

   public void openImageLibrary() {
    // ImageLibrary is the class where I code the stuff for the other 
    // activity
    Intent intent = new Intent(this, ImageLibrary.class);
    startActivityForResult(intent, 0);
}

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

    // result for when select is pressed and user has chosen empty meme
    if(requestCode == 0 && resultCode == RESULT_OK && data != null) {
        byte[] byteArray = getIntent().getByteArrayExtra("image");
    Bitmap meme = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);


        // Below, memeImage is the ImageView for the main activity
        ImageView imageView = (ImageView) findViewById(R.id.memeImage);
        imageView.setImageBitmap(meme);
    }
}

On other Activity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_library);
    }

    public void selectImage(View view) {
        Intent intent = new Intent();


        view.setDrawingCacheEnabled(true);


        view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

        view.buildDrawingCache(true);
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false); // clear drawing cache

    //Convert to byte array
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

        intent.putExtra("image", byteArray);
        setResult(RESULT_OK, intent);
        finish();
    }
Varun Malhotra
  • 1,202
  • 3
  • 15
  • 28
  • I am getting the same error as I was before. When I select the image, it gets bigger and I remain on the other activity's screen. When I try to debug this, I am getting errors saying: "Sources for 'Android API 26' not found". Also, I am getting an error saying: "Source code doesn't match byte code". When I continue with the debugger, I eventually get an error that says it attempted to get length of a null array and it failed to deliver the result (I'm guessing the intent didn't work by going back to the main activity) – Zelda Dec 22 '17 at 19:03
  • you have to configure setings in given link https://stackoverflow.com/questions/36814755/sources-for-android-api-23-platform-not-found-android-studio-2-0 answer with 148 votes you have to do the same with api 26 package – Varun Malhotra Dec 23 '17 at 05:26