繁体   English   中英

如何访问从网络下载的图像

[英]How to access image downloaded from the web

我使用此代码从网络下载了图像,并将其直接保存到应用程序特定的内部存储中。

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()方法的文档中所说:

返回到使用openFileOutput(String,int)创建的文件存储在文件系统上目录的绝对路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM