我試圖使用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
如果您收到任何錯誤,請發佈您的定位堆棧跟蹤。 – RajSharma
沒有錯誤。不在日誌或編譯時。只是我沒有在內部存儲中獲取圖像文件。如上面的代碼一樣敬酒。 –
您確定您正在編寫的映像文件的大小是否與您正在寫入外部存儲的字節[]的大小相同? – RajSharma