未将位图设置为imageview
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了未将位图设置为imageview相关的知识,希望对你有一定的参考价值。
我已经尝试了所有的东西,看了很多stackoverflow帖子,我似乎无法找到解决方案。
我正在制作一个模因生成器,在主要活动中我可以按下一个名为选择图像的按钮。这将带我到另一个活动,在那里我可以选择许多图片。我希望能够点击一个图像,然后让它返回到主要活动,并显示在图像视图中,模因是假设的。
这是我主要活动的代码:
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();
}
答案
试试下面的内容
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();
}
以上是关于未将位图设置为imageview的主要内容,如果未能解决你的问题,请参考以下文章
SeekBar:从 Drawable 获取空位图,直到将其设置为 ImageView 或如何从 9patch 获取结果位图
如何在来自 Firebase 存储的片段 ImageView 中显示图像