为了账号安全,请及时绑定邮箱和手机立即绑定

java.lang.RuntimeException: Canvas: 试图绘制过大

java.lang.RuntimeException: Canvas: 试图绘制过大

慕森王 2021-08-04 17:44:06
我正在尝试将图像从 Web 服务器检索到我的应用程序中。但是,当我在应用程序中查看图像时,应用程序崩溃了。这是代码:class DownloadFileFromURL extends AsyncTask<String, String, String> {    private String file_url=null;    /**     * Before starting background thread     * Show Progress Bar Dialog     * */    @Override    protected void onPreExecute() {        super.onPreExecute();        toggleProgress();    }    public boolean dir_exists(String dir_path)    {        boolean ret = false;        File dir = new File(dir_path);        if(dir.exists() && dir.isDirectory())            ret = true;        return ret;    }    /**     * Downloading file in background thread     * */    @Override    protected String doInBackground(String... f_url) {        int count;        try {            URL url = new URL(f_url[0]);            URLConnection conection = url.openConnection();            conection.connect();            // this will be useful so that you can show a tipical 0-100% progress bar            int lenghtOfFile = conection.getContentLength();            // download the file            InputStream input = new BufferedInputStream(url.openStream(), 8192);            String dir_path = Environment.getExternalStorageDirectory().toString() + getResources().getString(R.string.DownloadFolder);            if (!dir_exists(dir_path)){                File directory = new File(dir_path);                if(directory.mkdirs()){                    Log.v("dir","is created 1");                }else{                    Log.v("dir","not created 1");                }                if(directory.mkdir()){                    Log.v("dir","is created 2");                }else{                    Log.v("dir","not created 2");                }            }else{                Log.v("dir","is exist");            }
查看完整描述

2 回答

?
噜噜哒

TA贡献1784条经验 获得超7个赞

根据设备的显示尺寸,在显示之前调整图像大小是一个很好的方法。试试这个:


//->reduce size of bitmap for displaying on imageView

Bitmap bmp = BitmapFactory.decodeFile(file.getPath());

float RealWidth = bmp.getWidth();

float RealHeight = bmp.getHeight();

int NewWidth, NewHeight;

if (imageView.getWidth() > imageView.getHeight()) {

    NewWidth = imageView.getWidth();

    NewHeight = (int) ((RealHeight * NewWidth) / RealWidth);

} else {

    NewHeight = imageView.getHeight();

    NewWidth = (int) ((RealWidth * NewHeight) / RealHeight);

}

bmp = Bitmap.createScaledBitmap(bmp, NewWidth, NewHeight, false);

imageView.setImageBitmap(bmp);


查看完整回答
反对 回复 2021-08-04
?
慕虎7371278

TA贡献1802条经验 获得超4个赞

在将图片文件解码到内存中之前,最好先读取图片的大小并将其与手机的大小进行比较,然后缩小并最终解码。



查看完整回答
反对 回复 2021-08-04
  • 2 回答
  • 0 关注
  • 165 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信