2013-01-25 245 views
1

我想下載並保存圖像到SD卡,有時它的工作,有時它不。我需要幫助,試圖瞭解爲什麼,我試圖谷歌,但我無法找到答案。下載並保存圖像到SD卡

所以這是我的代碼來檢索雜誌問題,然後遍歷json數組中的每個項目,然後下載並將它們存儲到SD卡中。然而,一些圖片沒有得到下載,它跳過來代替(查看logcat的)

/** 
    * Background Async Task to Load all product by making HTTP Request 
    * */ 
    class downloadMagazine extends AsyncTask<String, String, String> { 

     @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       progressDialog = new ProgressDialog(Store.this); 
       progressDialog.setMessage("Loading.." + "\n" + "加載中.."); 
       progressDialog.setIndeterminate(false); 
       progressDialog.setCancelable(false); 
       progressDialog.show(); 
      } 
     /** 
     * getting preview url and then load them 
     * */ 
     protected String doInBackground(String... args) { 
      URL myFileUrl = null; 

      // Building Parameters 
       List<NameValuePair> params = new ArrayList<NameValuePair>(); 
       // getting JSON string from URL 
       JSONObject json = jParser.makeHttpRequest(url_magazine, "GET", params); 

       // Check your log cat for JSON reponse 
       //Log.d("All Products: ", json.toString()); 

       try { 
        // Checking for SUCCESS TAG 
        int success = json.getInt(TAG_SUCCESS); 

        if (success == 1) { 
         // products found 
         // Getting Array of Products 
         mag = json.getJSONArray(TAG_MAGAZINE); 

         for (int i = 0; i < mag.length(); i++) { 
          JSONObject c = mag.getJSONObject(i); 

          // Storing each json item in variable 
          String magazineUrl = c.getString(TAG_MAGAZINE_URL); 
          String issueName = c.getString(TAG_MAGAZINE_NAME); 

          urlList.add(magazineUrl); 
          issueNameList.add(issueName); 
          //System.out.println(urlList); 
         }     
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       }   

       // Building Parameters 
       List<NameValuePair> param = new ArrayList<NameValuePair>(); 
       // getting JSON string from URL 
       JSONObject json1 = jParser.makeHttpRequest(urlList.get(pos), "GET", param); 

       // CHECKING OF JSON RESPONSE 
       Log.d("All guide: ", json.toString()); 

       try { 
        issues = json1.getJSONArray(TAG_ISSUE); 

        for (int i = 0; i < issues.length(); i++) { 
         JSONObject c = issues.getJSONObject(i); 

         String image = c.getString(TAG_IMAGE); 

         imageList.add(image); 
         //System.out.println(imageList); 
        } 


        // STOP THE LOOP 
        //break; 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

       for (int i = 0; i < imageList.size(); i ++) 
       { 
       imageUrl = imageList.get(i).toString(); 
       Log.d("thumbnail", imageUrl); 
       number = i; 
       try { 
        myFileUrl = new URL(imageUrl); // RETRIEVE IMAGE URL 
        } 
       catch (MalformedURLException e) { 
        e.printStackTrace(); 
       } 
       try { 
        HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection(); 
        conn.setDoInput(true); 
        conn.connect(); 
        InputStream in = conn.getInputStream(); 
        Log.i("I'm connected", "Download"); 
        bmImg = BitmapFactory.decodeStream(in); 

        File filename; 
        // GET EXTERNAL STORAGE, SAVE FILE THERE 
        File storagePath = new File(Environment.getExternalStorageDirectory(),folderName+"issue"+issueNumber+"/"); 
        storagePath.mkdirs(); 

        filename = new File(storagePath + "/page"+number+".jpg"); 
        FileOutputStream out = new FileOutputStream(filename); 
        bmImg.compress(Bitmap.CompressFormat.JPEG, 90, out); 

         out.flush(); 
         out.close(); 

         MediaStore.Images.Media.insertImage(getContentResolver(),filename.getAbsolutePath(), filename.getName(), 
           filename.getName());   
         in.close(); 

         // displayImage(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       } 

       return null; 

} 

這是每當它跳過下載頁面(我知道這是類似我張貼,但我真的這個問題我的一個logcat的輸出在故障排除時需要幫助)。我不知道發生了什麼,我用Google搜索,發現採用了android 4.0開始面臨這個問題

01-23 17:09:29.120: W/System.err(24339): java.io.EOFException 
01-23 17:09:29.120: W/System.err(24339): at libcore.io.Streams.readAsciiLine(Streams.java:203) 
01-23 17:09:29.120: W/System.err(24339): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:544) 
01-23 17:09:29.120: W/System.err(24339): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:784) 
01-23 17:09:29.120: W/System.err(24339): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274) 
01-23 17:09:29.120: W/System.err(24339): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168) 
01-23 17:09:29.130: W/System.err(24339): at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:270) 
01-23 17:09:29.130: W/System.err(24339): at com.sfcca.coverflow.Store$downloadMagazine.doInBackground(Store.java:881) 
01-23 17:09:29.130: W/System.err(24339): at com.sfcca.coverflow.Store$downloadMagazine.doInBackground(Store.java:1) 
01-23 17:09:29.130: W/System.err(24339): at android.os.AsyncTask$2.call(AsyncTask.java:264) 
01-23 17:09:29.130: W/System.err(24339): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
01-23 17:09:29.130: W/System.err(24339): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
01-23 17:09:29.130: W/System.err(24339): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 
01-23 17:09:29.130: W/System.err(24339): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
01-23 17:09:29.130: W/System.err(24339): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
01-23 17:09:29.130: W/System.err(24339): at java.lang.Thread.run(Thread.java:856) 

回答

0

檢查是否有適當的權限到清單

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

非常感謝您的回覆。是的,我已經檢查過,我確實設置了這些權限 – user1234555

0

許多人一看這:http://code.google.com/p/google-http-java-client/issues/detail?id=116

我想嘗試的最後註釋那裏,看看問題是否會消失:

System.setProperty(「http.keepAlive」,「false」);現在

,你不一定要在長期的設置,但至少可以看到,如果這是相關的。

而且注意,雖然這個問題指的是彈簧和一堆其他的東西,這似乎是同根生的問題:Spring Rest Template usage causes EOFException

最後,我不完全知道你正在嘗試做在那裏,但是有一個AsyncTask做了很多工作(並沒有真正處理像網絡消失等突發事件,等等),但是你可能會在其他幾個方面遇到麻煩(與你的問題無關)。您可能還想考慮一個通用圖像下載/緩存庫,如果這可能有幫助,例如https://github.com/koush/UrlImageViewHelper。即使你不使用其中的一種,通過挖掘源代碼,你可能會看到它們是如何使事情變得更健壯。

+0

這是否解釋了爲什麼我下載圖像並將其保存到SD卡中的速度如此之慢?因爲我將它們全部組合成一個異步任務?我試着看看你給我的鏈接,但對我來說太複雜了。但我會嘗試修改我的代碼以使其更好 – user1234555