我正在尝试使用AsyncHttpClient library by loopj从网址下载图片资源,我不知道为什么我无法将其保存到内部存储空间。我的问题是,在运行下面的代码时,我没有在内部存储中看到任何图像文件,但是在日志或编译时我没有看到任何错误。以下两个Toast语句都有效。这是我正在使用的代码。
代码:
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 :(得分:0)
需要注意的一点,
您必须确保没有以前已存在的同名目录。在创建FileOutputStream之前尝试删除目录。
使用new FileAsyncHttpResponseHandler(Context)
构造函数,它会自动在您应用的缓存目录中创建临时文件,然后您可以移动文件,重命名,修改内容或删除它。
答案 1 :(得分: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;
}
此代码下载图像并将其保存在您想要的目录和名称中。使用它并分享任何错误。