未将位图设置为imageview

时间:2017-12-22 03:59:53

标签: java android bitmap

我已经尝试了所有内容,并查看了大量的stackoverflow帖子,我似乎无法为此找到解决方案。

我正在制作一个meme生成器,在主要活动中我可以按下一个名为select image的按钮。这将带我到另一个活动,在那里我可以选择许多图片。我希望能够点击一个图像,然后让它返回到主要活动,并显示在图像视图中,模因是假设的。

这是我的主要活动代码:

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

这是我的其他活动的代码:

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

1 个答案:

答案 0 :(得分:2)

尝试以下

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

关于其他活动

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