英文:
"Canvas: trying to draw too large bitmap" problem in Android Studio
问题
从网站获取bitmap
时,出现了“Canvas: trying to draw too large”问题。
因此,我在谷歌上搜索了这个问题,许多人写了他们的解决方案,但这些解决方案是关于目录drawable
中的位图文件的。
- 如果从网站获取的位图图像太大怎么办?
这是我的代码。
Thread mThread = new Thread() {
@Override
public void run() {
try {
URL url = new URL(postImgUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream inputStream = conn.getInputStream();
postImgBitmap = BitmapFactory.decodeStream(inputStream);
inputStream.close();
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
mThread.start();
mThread.join();
if (postImgBitmap != null){
postImg.setImageBitmap(postImgBitmap);
当问题出现时,变量postImgUrl
为"http://www.hstree.org/data/ck_tmp/202001/de3kOOtFqV6Bc2o7xCa7vg1UwFWJ.jpg",变量postImg
是ImageView
。
请给我建议。
英文:
When I try to get bitmap
from website, Canvas: trying to draw too large
problem is occured.
So I search this problem on google and many people wrote their solutions, but that solution is about bitmap file in directory drawable.
- What should I do if the bitmap image taken from the website is too large?
This is my code.
Thread mThread = new Thread() {
@Override
public void run() {
try {
URL url = new URL(postImgUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream inputStream = conn.getInputStream();
postImgBitmap = BitmapFactory.decodeStream(inputStream);
inputStream.close();
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
mThread.start();
mThread.join();
if (postImgBitmap != null){
postImg.setImageBitmap(postImgBitmap);
When a problem occurs, variable postImgUrl is "http://www.hstree.org/data/ck_tmp/202001/de3kOOtFqV6Bc2o7xCa7vg1UwFWJ.jpg" and variable postImg is ImageView.
Please advise me.
答案1
得分: 0
以下是翻译好的内容:
错误意味着位图的大小对于ImageView来说太大了。
在将位图设置到ImageView之前,您可以使用Bitmap.createScaledBitmap()
将大的位图缩放为较小的位图。然后,您可以安全地将位图设置到ImageView上。
示例代码:
public class MainActivity extends AppCompatActivity {
private static final String imageUrl
= "http://www.hstree.org/data/ck_tmp/202001/de3kOOtFqV6Bc2o7xCa7vg1UwFWJ.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView = findViewById(R.id.imageView);
new Thread(new Runnable() {
@Override
public void run() {
URL url = null;
try {
url = new URL(imageUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try (BufferedInputStream bufferedInputStream
= new BufferedInputStream(url.openStream())) {
Bitmap bitmap = BitmapFactory.decodeStream(bufferedInputStream);
final Bitmap scaledBitmap = Bitmap.createScaledBitmap(
bitmap,
(int) (bitmap.getWidth() * 0.1),
(int) (bitmap.getHeight() * 0.1),
true
);
runOnUiThread(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(scaledBitmap);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
注意:我已经去掉了HTML编码中的"
,以便代码在Java中正确显示。
英文:
The error means the size of Bitmap is too large for the ImageView.
Before setImageBitmap()
to ImageView, you can scale the large Bitmap to a smaller one with Bitmap.createScaledBitmap()
. Then you can safely set the Bitmap to ImageView.
A sample code:
public class MainActivity extends AppCompatActivity {
private static final String imageUrl
= "http://www.hstree.org/data/ck_tmp/202001/de3kOOtFqV6Bc2o7xCa7vg1UwFWJ.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView = findViewById(R.id.imageView);
new Thread(new Runnable() {
@Override
public void run() {
URL url = null;
try {
url = new URL(imageUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try (BufferedInputStream bufferedInputStream
= new BufferedInputStream(url.openStream())) {
Bitmap bitmap = BitmapFactory.decodeStream(bufferedInputStream);
final Bitmap scaledBitmap = Bitmap.createScaledBitmap(
bitmap,
(int) (bitmap.getWidth() * 0.1),
(int) (bitmap.getHeight() * 0.1),
true
);
runOnUiThread(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(scaledBitmap);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论