简体   繁体   中英

Bitmap is not being set to the imageview

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM