Search code examples
androidfile-iostorageinternal

android - storing image in internal storage


I an trying to store images downloaded from web to internal storage. I am refer following solution android - storing image cache in internal memory and reusing it

but still i am getting exception :

07-19 12:05:47.729: E/AndroidRuntime(341): java.lang.IllegalArgumentException: File /data/data/com.yellow.activity/files/-1717792749 contains a path separator

How to load image from filepath : here is my code . image is an arraylist of URLs.

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

how to load image to imageView? I tried.

        File filePath = getFileStreamPath(img_path.get(i));
        imageView.setImageDrawable(Drawable.createFromPath(filePath.toString()));

but it didn't work.


Solution

  • I solved it. Replace following code

    File filePath = getFileStreamPath(img_path.get(i)); 
    imageView.setImageDrawable(Drawable.createFromPath(filePath.toString()));
    

    with

    imageView.setImageDrawable(Drawable.createFromPath(img_path.get(i)));