- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Activity ,您可以在其中绘制位图,并使用 Intent 将其发送到下一个 Activity 并将其放入其中的 ImageView 中。由于某种原因,它不会产生错误,但也不会将图像设置为应有的样子。当宽度和高度不兼容时,这会成为问题吗?
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent i = new Intent(getApplicationContext(), PrintActivity.class);
i.putExtra("bitmap", byteArray);
startActivity(i);
我只想提一下,此方法适用于其他 Activity ,因为我多次跨 Activity 发送位图。
获取 Intent 的 Activity :
img = findViewById(R.id.img);
byte[] byteArray = getIntent().getByteArrayExtra("bitmap");
if (byteArray.length > 0) {
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); // In a different thread this seemed to help the OP but it didn't in my case
img.setImageBitmap(bmp);
我还尝试将位图保存到图库,但出现了多个错误,其中一些错误表明位图为空。虽然我不明白这是怎么回事,因为它是 Canvas 的位图。我用这个获取它:
public Bitmap getBitmap() {
return mBitmap;
}
mbitmap 是用来在 Canvas 上书写的:
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
我做错了什么?
最佳答案
我看到您通过压缩为字节数组来移动位图,然后再次解码它们。请注意,BitMaps 实现了 Parcelable
,这很好,因为您可以使用
Intent intentions = new Intent(this, someActivity.class);
intentions.putExtra("parcelable_photo", mPhoto);
获取包裹
Intent received = getIntent();
Bitmap mPhoto = (Bitmap) received.getExtras().getParcelable("parcelable_photo");
这应该比您当前的方法更快、更少的打字工作。
要在 ImageView 中设置此位图,请执行以下操作:
ImageView mImg = findViewById(R.id.img_id);
img.setImageBitmap(mPhoto);
这是source对于setImageBitmap()
。
我不知道ImageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
,但我希望上述方法能够解决问题。
如果问题仍然存在,请随时发表评论
关于java - 看来 ImageView.setImageBitmap 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54712503/
我想显示从 MySQL 检索到的图像到 android imageView(image),但是获取 cannot resolve method setImageBitmap error . pri
我有一个字节数组,其中包含从网上获取的图像。我使用 Bitmapfactory、BitmapDrawable 和 setImageDrawable 将它们懒惰地加载到我的 UI Activity 中(
单击时,我的应用程序会在相机和画廊之间进行选择,然后该图片会显示在 ImageView 中。我最初尝试显示完整图像,然后尝试使用位图方式但没有任何效果。我只是得到一个空白的 ImageView。请给我
我有 ImageView,它应该使用以下代码填充 Gallery 中的图像: Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
我使用 MediaStore 拍了一张照片并保存了它。我可以在我的图库中看到图像。但是,当我尝试将图像位图设置到我的 ImageView 时,它变成空白。 String abspath; public
我正在练习使用 Camera API,为此我执行了以下操作: 一个。为捕获的图像设置一个目录(用于startActivityForResult) 设置位图,以便图像一旦保存在应用程序本身中就可以显示。
假设我的代码类似于下面的代码: protected void onCreate(Bundle bundle){ this.imageView = (ImageView) contentView
我有一个 Activity ,您可以在其中绘制位图,并使用 Intent 将其发送到下一个 Activity 并将其放入其中的 ImageView 中。由于某种原因,它不会产生错误,但也不会将图像设置
我正在尝试从 interwebz 加载图像,但出现空指针异常。这是我的代码: public class Search extends ActionBarActivity { TableLayout t
我正在开发一个简单的 Android 应用程序,它在单独的线程中下载图像,然后在主 Activity 中更新 imageViews。它工作完美,但是当我在下载时旋转屏幕时,imageView 不再随图
在我的应用程序中,用户选择一个位图,然后我使用以下代码: BitmapFactory.Options op= new BitmapFactory.Options(); Bitmap bitmap =
我正在做一些实验,我正在尝试做这样的事情: public class MyActivity extends Activity { @Override protected void on
我正在尝试从网站下载图片。这是我的代码:主要 Activity ... public class MainActivity extends Activity { public final static
如果我预加载某些图像,这对我的应用程序是有利的。正如官方文档中所写,我在 AsyncTask 中正确地执行了此操作。但是我对何时设置它们有疑问。 我将展示代码 fragment 。请注意,它是简化的(
我有这个代码: 和 imageview_logo.setImageBitmap(bit); // comes from assets imageview_logo.setAdjustViewBoun
我正在为 Android 开发一个应用程序,我正在使用 Gallery 小部件,我已将其调整为全屏模式,因此它一次显示一张图像。 这是我的 Activity 代码的一部分: public View
我写了一个小应用程序,它应该显示一个转速表,获取它关于转速的数据......通过蓝牙。当数据发生变化时,我会旋转针和其他一些 imageViews 以在屏幕上显示当前值。 旋转代码: bMapRpm=
setImageBitmap和setImageDrawable有什么区别? 我有一张我想从文件中动态设置的图像。我遵循的教程说将我的 Bitmap 转换为 BitmapDrawable 然后使用 se
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 7 年前。 我有一种方法可以读取外部存储空间
我是 android 开发的新手。我有一个关于我的应用程序中图像缩放的问题。当我使用 setImageDrawable() 设置图像时,图像看起来像屏幕截图中那样: 但是如果我使用 setImageB
我是一名优秀的程序员,十分优秀!