在我的应用程序中,我试图将图像存储在内部存储器中,以便它只能在我的应用程序中使用,并且不能以其他方式看到。
通过以下方式,我将图像缓存存储在内部存储器中
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 是一个数组列表,其中包含我尚未下载的图像 url。img_path 是一个数组列表,我在其中存储存储图像缓存的路径。
存储的路径似乎如下
/data/data/com.intr.store/app_1219784788
那条带有我的包名的路径,这是正确的吗?我没有给app_任何地方,但它是怎么来的?
在我的其他一项活动中,我想将其加载到图像视图中。我已经通过以下方式尝试过
File filePath = getFileStreamPath(pth);
        i.setImageDrawable(Drawable.createFromPath(filePath.toString()));
这里 pth 是路径,i 是图像视图。但是应用程序崩溃了
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