亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 186 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號