简体   繁体   English

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

[英]How to access image downloaded from the web

I downloaded an image from the web using this code and saved it directly to the application specific internal storage. 我使用此代码从网络下载了图像,并将其直接保存到应用程序特定的内部存储中。

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);

How do I access this stored image using Drawable ? 如何使用Drawable访问此存储的图像? I attempted to open it using the following code but the operation resulted in a file not found exception. 我尝试使用以下代码打开它,但该操作导致文件未找到异常。

File imgPath = new File(id + ".jpg");
profilePic.setImageDrawable(Drawable.createFromPath(imgPath.getPath()));

Assuming you call os.close() in the following lines of your code 假设您在代码的以下几行中调用os.close()

You need to specify the path when instantiating your File object. 实例化File对象时,需要指定路径。 Do the following : 请执行下列操作 :

File imgPath = new File(yourContext.getFilesDir(), id + ".jpg");

As said in doc of getFilesDir() method : 如在getFilesDir()方法的文档中所说:

Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored. 返回到使用openFileOutput(String,int)创建的文件存储在文件系统上目录的绝对路径。

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

相关问题 如何在android中更改svg图像的颜色。 图片是从 web api 下载的 - How to change color of svg image in android. Images are downloaded from web api 从网络下载的列表视图中行的图像应更小 - Image downloaded from web for row in listview smaller as it should be 将listview项目背景图像设置为从Web下载的可绘制对象 - Setting listview item background image to drawable downloaded from the web 如何在Java中缓存从URL下载的图像? - How to cache image downloaded from URL in Java? 如何将下载的图像从一个活动传递到下一个活动? - How do I pass a downloaded image from one activity to the next? 如何从服务器替换存储中具有相同文件名的下载图像? - How to replace downloaded image with same file name in storage from server? 保存从Parse.com下载的图像 - Save image downloaded from Parse.com 在ImageView中显示从数据库下载的图像 - Displaying a Downloaded Image From Database in ImageView 如何在android中检索下载的数据以供离线访问? - How to retrieve the downloaded data for the offline access in android? 在使用 Selenium Grid 时,我们如何从 Hub 机器访问 Node 机器中下载的文件? - While using Selenium Grid, How do we access downloaded file in Node machine from Hub machine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM