“decoder->decode returned false” when download image and view it in ImageView

…衆ロ難τιáo~ 提交于 2019-12-08 04:07:40

问题


I try to use FlushedInputStream : Android decoder->decode returned false for Bitmap download

but nothing change, because i use: BitmapFactory.decodeFile(path_of_my_downloaded_file), not use BitmapFactory.decodeStream

This is my code of download file:

  public static boolean downloadFile(String url, String dir, String name){
         Log.i("Start Downloading ", "=");
       //    Create download folder:
         File f = new File(dir);
         if(!f.exists()){
             f.mkdirs();
         }
        try {
            File fTo = new File(dir, name);
            URL downloadUrl = new URL(url);
            //create the new connection
            HttpURLConnection urlConnection = (HttpURLConnection) downloadUrl.openConnection();
            //set up some things on the connection
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            //and connect!
            urlConnection.connect();
            FlushedInputStream in = new FlushedInputStream(downloadUrl.openStream());

//          in = new FlushedInputStream(in);

            byte[] buffer= new byte[4096];
//          Write file to toFolder
            FileOutputStream os = new FileOutputStream(fTo);
            try {
                 do{
                     int numread = in.read(buffer);  
                     if (numread <= 0)  {
                         break;
                     } 
                     os.write(buffer, 0, numread);
                }while(true);
            } catch (ConnectTimeoutException e) {
                e.printStackTrace();
                return false;

            }
            if (os != null) {
                os.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            Log.e("Error reading file", e.toString());
            return false;
        }
        return true;
     }

And this is my code to set Bitmap to ImageView:

Bitmap bitmap = BitmapFactory.decodeFile(my_file);
mImageView.setImageBitmap(bitmap);

I always have "decoder->decode returned false"

Note: I have to download this image first.


回答1:


This is the problems of image.



来源:https://stackoverflow.com/questions/9306034/decoder-decode-returned-false-when-download-image-and-view-it-in-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!