2015-06-29 38 views
0

我試圖使用AsyncHttpClient library by loopj從URL下載圖像資源,我不知道爲什麼我無法將其保存到內部存儲。我的問題是我沒有看到內部存儲器上運行下面的代碼中的任何圖像文件,但我沒有看到日誌或編譯時的任何錯誤。以下Toast聲明均適用。這是我正在使用的代碼。我是新的在stackoverflow,所以請裸露在我身邊。使用異步Http客戶端庫下載圖像

代碼:

String[] allowedTypes = new String[] { "image/png" }; 
    AsyncHttpClient client = new AsyncHttpClient(); 
    client.get("http://example.com/dock.png", 
      new BinaryHttpResponseHandler(allowedTypes) { 
       @Override 
       public void onSuccess(int i, Header[] headers, byte[] bytes) { 

        Toast.makeText(getApplicationContext(),"Successful in finding file",Toast.LENGTH_SHORT).show(); 
        try { 
         Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_SHORT).show(); 
         FileOutputStream f = new FileOutputStream((new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + "imgdwo.png"))); 
         f.write(bytes); //your bytes 
         f.close(); 
        } catch (FileNotFoundException e) { 
         e.printStackTrace(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 

       @Override 
       public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) { 

       } 


    }); 

更新:我在內部存儲器改變了硬編碼的位置,但不解決問題。該logcat的低於:

java.io.FileNotFoundException: /storage/emulated/0/Pictures: open failed: EISDIR (Is a directory) 
06-29 09:07:28.721 17675-17675/com.example.abhishek.asynchttpclient W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:416) 
06-29 09:07:28.721 17675-17675/com.example.abhishek.asynchttpclient W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:88) 
06-29 09:07:28.721 17675-17675/com.example.abhishek.asynchttpclient W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:128) 
06-29 09:07:28.721 17675-17675/com.example.abhishek.asynchttpclient W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:117) 
06-29 09:07:28.721 17675-17675/com.example.abhishek.asynchttpclient W/System.err﹕ at com.example.abhishek.asynchttpclient.AsyncActivity$1.onSuccess(AsyncActivity.java:41) 
06-29 09:07:28.721 17675-17675/com.example.abhishek.asynchttpclient W/System.err﹕ at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:311) 
06-29 09:07:28.721 17675-17675/com.example.abhishek.asynchttpclient W/System.err﹕ at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:138) 
06-29 09:07:28.721 17675-17675/com.example.abhishek.asynchttpclient W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:99) 
06-29 09:07:28.721 17675-17675/com.example.abhishek.asynchttpclient W/System.err﹕ at android.os.Looper.loop(Looper.java:137) 
06-29 09:07:28.721 17675-17675/com.example.abhishek.asynchttpclient W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5293) 
06-29 09:07:28.721 17675-17675/com.example.abhishek.asynchttpclient W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method) 
06-29 09:07:28.721 17675-17675/com.example.abhishek.asynchttpclient W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511) 
06-29 09:07:28.721 17675-17675/com.example.abhishek.asynchttpclient W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
06-29 09:07:28.721 17675-17675/com.example.abhishek.asynchttpclient W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
06-29 09:07:28.721 17675-17675/com.example.abhishek.asynchttpclient W/System.err﹕ at dalvik.system.NativeStart.main(Native Method) 
06-29 09:07:28.721 17675-17675/com.example.abhishek.asynchttpclient W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: EISDIR (Is a directory) 
06-29 09:07:28.721 17675-17675/com.example.abhishek.asynchttpclient W/System.err﹕ at libcore.io.Posix.open(Native Method) 
06-29 09:07:28.726 17675-17675/com.example.abhishek.asynchttpclient W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) 
06-29 09:07:28.726 17675-17675/com.example.abhishek.asynchttpclient W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:400) 
06-29 09:07:28.726 17675-17675/com.example.abhishek.asynchttpclient W/System.err﹕ ... 14 more 
+0

如果您收到任何錯誤,請發佈您的定位堆棧跟蹤。 – RajSharma

+0

沒有錯誤。不在日誌或編譯時。只是我沒有在內部存儲中獲取圖像文件。如上面的代碼一樣敬酒。 –

+0

您確定您正在編寫的映像文件的大小是否與您正在寫入外部存儲的字節[]的大小相同? – RajSharma

回答

0

點要注意的,

  1. 你必須確保,存在與之前已經存在相同名稱的目錄。嘗試在創建FileOutputStream之前刪除該目錄。

  2. 使用new FileAsyncHttpResponseHandler(Context)構造函數,它會自動在您的應用程序的緩存目錄中創建臨時文件,然後您可以移動文件,重命名它,修改其內容或刪除它。

+0

我沒有任何文件具有相同的名稱。我渴望找到一個。 –

0

使用此代碼創建目錄如果不存在並在創建文件之後。

     Bitmap mIcon; 
         File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
if (!dir.exists()) { 
        //noinspection ResultOfMethodCallIgnored 
        dir.mkdir(); 
       } 
String fname = "myimage.png"; 
File myimage = new File(dir, fname); 
         try { 
         InputStream in = new java.net.URL(imageurl).openStream(); 
         mIcon = BitmapFactory.decodeStream(in); 

         try { 
          FileOutputStream out = new FileOutputStream(myimage); 
          mIcon.compress(
            Bitmap.CompressFormat.JPEG, 
            100, out); 
          out.flush(); 
          out.close(); 
          return myimage; 

         } catch (FileNotFoundException e) { 
          return null; 
         } catch (IOException e) { 
          return null; 
         } 

        } catch (Exception e) { 
         return null; 
        } 

該代碼下載映像並將其保存在您想要的目錄和名稱中。使用它並分享任何錯誤。

+0

我應該在哪裏檢查文件,因爲我仍然無法找到它? –

+0

你想要什麼?驗證文件是否存在?上面的代碼創建文件,如果你的字節不正確,你的文件不會被創建。更新代碼並共享新的logcat。 – Santiago

+0

@JawaharSingh我用更完整的代碼更新答案,如何從url下載和保存圖像。 – Santiago