2012-07-19 121 views
4

我試圖將從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())); 

但它沒有工作。

+0

請添加您urlStr字符串在這裏.. ???意味着你通過這種方法傳遞的信息。 – 2012-07-19 07:38:05

+0

你在這個文件中得到了什麼路徑wyinMyDir.getAbsolutePath() – Nirali 2012-07-19 07:38:53

+0

我得到的路徑是/data/data/com.yellow.activity/files/-1718716270 – Roshni 2012-07-19 08:15:57

回答

0

我解決它。 替換下面的代碼

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

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

嘗試這種方式

String PATH = fileWithinMyDir.getAbsolutePath() + filename; 
+0

仍然得到異常th java.lang.IllegalArgumentException:File/data/data/com .yellow.activity/files-1717792749包含一個路徑分隔符 – Roshni 2012-07-19 08:02:26

1

要保存到內部存儲器...

File fileWithinMyDir = getApplicationContext().getFilesDir(); 
    try 
      { 
       StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
       .permitAll().build(); 
      StrictMode.setThreadPolicy(policy); 
      URL url = new URL("http://t2.gstatic.com /images?q=tbn:ANd9GcQjZgUffqqe2mKKb5VOrDNd-ZxD7sJOU7WAHlFAy6PLbtXpyQZYdw"); 
      File file = new File(fileWithinMyDir.getAbsolutePath() + "/" +"sun.jpg"); 
      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()); 
     } 

爲了從內部存儲器加載圖像..

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
      .permitAll().build(); 
      StrictMode.setThreadPolicy(policy); 
      mImgView1 = (ImageView) findViewById(R.id.mImgView1); 

      Bitmap bitmap = BitmapFactory.decodeFile(fileWithinMyDir.getAbsolutePath() + "/" +"sunn"+".file extension"); 
      mImgView1.setImageBitmap(bitmap); 

這是較新的Android的,顯示錯誤原因是「android.os.networkonmainthreadexception」

ü也可以使用的AsyncTask如果u要解決的問題......

+0

,這對我來說真的很合適,謝謝! – edwinj 2015-07-14 22:38:58

相關問題