我正在尝试将图像从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");
}
// Output stream
OutputStream output = new FileOutputStream(dir_path+title.toString().replace("/","_")+"."+extension);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
output.close();
input.close();
this.file_url = dir_path + title.toString().replace("/", "_") + "." + extension;
if (blurvalue!=0){
File file = new File(this.file_url);
Log.v("BASSAM","NEW FILE");
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Log.v("BASSAM","OPTIONS");
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(),bmOptions);
Log.v("BASSAM","bitmap");
bitmap = bitmapBlur;
Log.v("BASSAM","fastblur");
try {
FileOutputStream out = new FileOutputStream(this.file_url);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
Log.v("BASSAM",e.getMessage());
}
}
这是错误:
java.lang.RuntimeException: Canvas: trying to draw too large(132389600bytes) bitmap.
at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:229)
at android.view.RecordingCanvas.drawBitmap(RecordingCanvas.java:97)
at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:529)
at com.squareup.picasso.PicassoDrawable.draw(PicassoDrawable.java:110)
at android.widget.ImageView.onDraw(ImageView.java:1367)
at android.view.View.draw(View.java:19192)
at android.view.View.updateDisplayListIfDirty(View.java:18142)
at android.view.View.draw(View.java:18920)
at android.view.ViewGroup.drawChild(ViewGroup.java:4236)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4022)
at android.view.View.updateDisplayListIfDirty(View.java:18133)
at android.view.View.draw(View.java:18920)
at android.view.ViewGroup.drawChild(ViewGroup.java:4236)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4022)
at android.view.View.draw(View.java:19195)
at android.view.View.updateDisplayListIfDirty(View.java:18142)
at android.view.View.draw(View.java:18920)
at android.view.ViewGroup.drawChild(ViewGroup.java:4236)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4022)
at android.view.View.draw(View.java:19195)
at android.view.View.updateDisplayListIfDirty(View.java:18142)
at android.view.View.draw(View.java:18920)
at android.view.ViewGroup.drawChild(ViewGroup.java:4236)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4022)
at android.view.View.updateDisplayListIfDirty(View.java:18133)
at android.view.View.draw(View.java:18920)
at android.view.ViewGroup.drawChild(ViewGroup.java:4236)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4022)
at android.view.View.updateDisplayListIfDirty(View.java:18133)
at android.view.View.draw(View.java:18920)
at android.view.ViewGroup.drawChild(ViewGroup.java:4236)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4022)
at android.view.View.updateDisplayListIfDirty(View.java:18133)
at android.view.View.draw(View.java:18920)
at android.view.ViewGroup.drawChild(ViewGroup.java:4236)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4022)
at android.view.View.updateDisplayListIfDirty(View.java:18133)
at android.view.View.draw(View.java:18920)
at android.view.ViewGroup.drawChild(ViewGroup.java:4236)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4022)
at android.view.View.updateDisplayListIfDirty(View.java:18133)
at android.view.View.draw(View.java:18920)
at android.view.ViewGroup.drawChild(ViewGroup.java:4236)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4022)
at android.view.View.draw(View.java:19195)
at com.android.internal.policy.DecorView.draw(DecorView.java:788)
at android.view.View.updateDisplayListIfDirty(View.java:18142)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:669)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:675)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:783)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2992)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2806)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2359)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1392)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6752)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
答案 0 :(得分:0)
在将图片文件解码到内存之前,最好先读取图片的大小,然后将其与手机的大小进行比较,缩小并最终对其进行解码。
查看评论中的链接。
答案 1 :(得分:0)
根据设备的显示尺寸,最好在显示前调整图像尺寸。试试这个:
//->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);