Android图片内存缓存组件,android-将图像缓存存储在内部存储器中并重...

在我的应用程序中,我试图将图像存储在内部存储器中,以便只能在我的应用程序中使用它,而不能以其他方式看到它.

我以以下方式将图像缓存存储在内部存储器中

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是一个arraylist,其中包含我尚未从中下载的图像URL.

img_path是一个arraylist,其中我正在存储存储图像缓存的路径.

存储的路径似乎如下

/data/data/com.intr.store/app_1219784788

带有我包裹名称的路径,这是正确的吗?我没有在任何地方提供该app_,但是它是怎么来的?

在另一项活动中,我想将其加载到图像视图中.我已经尝试过以下方式

File filePath = getFileStreamPath(pth);

i.setImageDrawable(Drawable.createFromPath(filePath.toString()));

在这里,pth是路径,而我是图像视图.但是应用程序崩溃了,说

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值