我使用此代码从网络下载了图像,并将其直接保存到应用程序特定的内部存储中。
is = url.openConnection().getInputStream();
os = LoginActivity.this.openFileOutput(id + ".jpg", Context.MODE_PRIVATE);
int read;
byte[] data = new byte[1024];
while ((read = is.read(data)) != -1)
os.write(data, 0, read);
如何使用 Drawable
访问此存储的图像?我尝试使用以下代码打开它,但操作导致文件未找到异常。
File imgPath = new File(id + ".jpg");
profilePic.setImageDrawable(Drawable.createFromPath(imgPath.getPath()));
最佳答案
假设您在以下代码行中调用 os.close()
实例化 File 对象时需要指定路径。执行以下操作:
File imgPath = new File(yourContext.getFilesDir(), id + ".jpg");
正如 getFilesDir()
方法的文档中所述:
Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.
关于java - 如何访问从网络下载的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20445567/