我试图将从web下载的图像存储到内部存储器。我是指以下解决方案android - storing image cache in internal memory and reusing itandroid-在内部存储器中存储图像
但仍然我得到异常:
07-19 12:05:47.729: E/AndroidRuntime(341): java.lang.IllegalArgumentException: File /data/data/com.yellow.activity/files/-1717792749 contains a path separator
如何从文件路径载入图片: 这里是我的代码。 图片是一个URL的数组列表。
File fileWithinMyDir = getApplicationContext().getFilesDir();
for(int i=0; i<image.size();i++){
String filename = String.valueOf(image.get(i).hashCode());
String urlString = image.get(i);
String PATH = fileWithinMyDir.getAbsolutePath() + "/" +filename;
infoLog(PATH);
DownloadFromUrl(PATH, urlString);
img_path.add(PATH);
}
private void DownloadFromUrl(String fileName, String urlStr)
{
try
{
URL url = new URL(urlStr);
File file = new File(fileName);
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1)
{
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream(file,true);
fos.write(baf.toByteArray());
fos.close();
infoLog("going ryt....");
}
catch (IOException e)
{
infoLog("download "+ e.getMessage());
}
}
如何将图像加载到imageView?我试过了。
File filePath = getFileStreamPath(img_path.get(i));
imageView.setImageDrawable(Drawable.createFromPath(filePath.toString()));
但它没有工作。
请添加您urlStr字符串在这里.. ???意味着你通过这种方法传递的信息。 – 2012-07-19 07:38:05
你在这个文件中得到了什么路径wyinMyDir.getAbsolutePath() – Nirali 2012-07-19 07:38:53
我得到的路径是/data/data/com.yellow.activity/files/-1718716270 – Roshni 2012-07-19 08:15:57