位图未设置为图像视图
Posted
技术标签:
【中文标题】位图未设置为图像视图【英文标题】:Bitmap is not being set to the imageview 【发布时间】:2018-06-04 18:18:55 【问题描述】:我已经尝试了所有方法并查看了许多 *** 帖子,但我似乎无法找到解决方案。
我正在制作一个模因生成器,在主要活动中我可以按下一个名为“选择图像”的按钮。这将带我参加另一项活动,在那里我会看到许多可供我选择的图片。我希望能够单击图像,然后让它返回到主要活动并显示在 meme 应该存在的图像视图上。
这是我的主要活动代码:
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();
【问题讨论】:
请发布您的错误 @RahulKhurana 对不起,我忘记了。我试图按照第一个答案所说的去做,并且我已经对错误的答案发表了评论。请看评论! 【参考方案1】:试试下面的
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();
【讨论】:
我遇到了和以前一样的错误。当我选择图像时,它会变大,并且我会留在其他活动的屏幕上。当我尝试对此进行调试时,我收到错误消息:“找不到‘android API 26’的来源”。另外,我收到一条错误消息:“源代码与字节码不匹配”。当我继续使用调试器时,我最终收到一个错误,说它试图获取空数组的长度,但未能提供结果(我猜测返回主要活动的意图不起作用) 您必须在给定链接***.com/questions/36814755/… 中配置设置以 148 票回答您必须对 api 26 包执行相同操作以上是关于位图未设置为图像视图的主要内容,如果未能解决你的问题,请参考以下文章