【问题标题】:android - storing image in internal storageandroid - 将图像存储在内部存储器中
【发布时间】:2012-07-18 08:17:06
【问题描述】:

我试图将从网络下载的图像存储到内部存储中。我参考以下解决方案android - storing image cache in internal memory and reusing it

但我仍然遇到异常:

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

如何从文件路径加载图像: 这是我的代码。 image 是 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()));

但是没有用。

【问题讨论】:

  • 请在此处添加您的 urlStr 字符串..???表示您在该方法中传递的内容。
  • 你在这个文件中得到什么路径WithinMyDir.getAbsolutePath()
  • 我得到的路径是/data/data/com.yellow.activity/files/-1718716270

标签: android file-io storage internal


【解决方案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.os.networkonmainthreadexception”而显示错误的较新的android

如果你想解决问题,你也可以使用 AsyncTask...

【讨论】:

    【解决方案2】:

    我解决了。 替换以下代码

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

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

    【讨论】:

      【解决方案3】:

      试试这个方法

      String PATH = fileWithinMyDir.getAbsolutePath() + filename;
      

      【讨论】:

      • 仍然出现 java.lang.IllegalArgumentException 异常:文件 /data/data/com.yellow.activity/files-1717792749 包含路径分隔符
      猜你喜欢
      • 2013-01-16
      • 2011-08-11
      • 1970-01-01
      • 2012-06-27
      • 1970-01-01
      • 2013-10-28
      • 2014-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多