Android的 - 存储在内部存储器中的图像缓存和重新使用 [英] android - storing image cache in internal memory and reusing it
问题描述
在我的应用程序我试图存储图像在内部存储器中,以便它可以使用仅在我的应用程序,并且可以以其他方式不可见。
在下面的方式,我已经存储在内部存储器中的图像缓存
文件cacheDir = getApplicationContext()GETDIR(,Context.MODE_PRIVATE)。
文件fileWithinMyDir =新的文件(cacheDir,);
的for(int i = 0; I< img.size();我++)
{
字符串文件名=将String.valueOf(img.get(我).hash code());
串urlString = img.get(ⅰ);
字符串PATH = fileWithinMyDir +文件名;
DownloadFromUrl(PATH,urlString);
img_path.add(PATH);
}
私人无效DownloadFromUrl(字符串文件名,字符串urlStr)
{
尝试
{
网址URL =新的URL(urlStr);
档案文件=新的文件(文件名);
URLConnection的UCON = url.openConnection();
InputStream的是= ucon.getInputStream();
的BufferedInputStream双=新的BufferedInputStream(是);
ByteArrayBuffer BAF =新ByteArrayBuffer(50);
INT电流= 0;
而((电流= bis.read())!= - 1)
{
baf.append((字节)电流);
}
FileOutputStream中FOS =新的FileOutputStream(文件);
fos.write(baf.toByteArray());
fos.close();
}
赶上(IOException异常E)
{
Log.e(下载,e.getMessage());
}
}
IMG是包含图片的URL从中我还没有下载一个ArrayList。 img_path是一个ArrayList中,我存储在图像缓存存储的路径。
所保存的路径似乎是如下:
/data/data/com.intr.store/app_1219784788
我的包名称这条道路,这是一个正确的?我还没有考虑到应用_
的任何地方,但它是如何来了?
在我的其他活动一:我想加载它在图像视图。我已经尝试过通过以下方式
文件文件路径= getFileStreamPath(PTH);
i.setImageDrawable(Drawable.createFromPath(filePath.toString()));
在这里PTH的路径和我的图像视图。但是,应用程序获取应声说
06-26 14:40:08.259:E / AndroidRuntime(6531):java.lang.IllegalArgumentException异常:产生的原因文件/data/data/com.intr.store/app_1219784788包含路径分隔符
您写的不对code。
替换
文件cacheDir = getApplicationContext()GETDIR(,Context.MODE_PRIVATE)。
文件fileWithinMyDir =新的文件(cacheDir,);
要
文件fileWithinMyDir = getApplicationContext()getFilesDir()。
然后
替换
字符串PATH = fileWithinMyDir +文件名;
要
字符串PATH = fileWithinMyDir.getAbsolutePath()+/+文件名+文件扩展名。;
in my app i am trying to store the images in internal memory so that it can be used only in my app and cannot be seen in other ways.
In the following way i have stored the image cache in internal memory
File cacheDir = getApplicationContext().getDir("", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(cacheDir, "");
for(int i = 0; i < img.size(); i++)
{
String filename = String.valueOf(img.get(i).hashCode());
String urlString = img.get(i);
String PATH = fileWithinMyDir + filename;
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);
fos.write(baf.toByteArray());
fos.close();
}
catch (IOException e)
{
Log.e("download", e.getMessage());
}
}
img is an arraylist which contains the images url from which i am yet to download. img_path is an arraylist in which i am storing the path where the image cache is been stored.
The stored path seems to be as follows
/data/data/com.intr.store/app_1219784788
that path with my package name, is this a right one ? i have not given that app_
anywhere, but how does it came ?
in one of my other activity i want to load it in image view. I have tried it in the following way
File filePath = getFileStreamPath(pth);
i.setImageDrawable(Drawable.createFromPath(filePath.toString()));
here pth is the path and i is the image view. But the app get crashed saying that
06-26 14:40:08.259: E/AndroidRuntime(6531): Caused by: java.lang.IllegalArgumentException: File /data/data/com.intr.store/app_1219784788 contains a path separator
You have written wrong code.
Replace
File cacheDir = getApplicationContext().getDir("", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(cacheDir, "");
To
File fileWithinMyDir = getApplicationContext().getFilesDir();
Then
Replace
String PATH = fileWithinMyDir + filename;
To
String PATH = fileWithinMyDir.getAbsolutePath() + "/" +filename+".file extension";
这篇关于Android的 - 存储在内部存储器中的图像缓存和重新使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!