4

Webからダウンロードした画像を内部ストレージに保存しようとしています。私は次のソリューションandroidを参照しています-画像キャッシュを内部メモリに保存して再利用します

しかし、それでも私は例外を取得しています:

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

しかし、それは機能しませんでした。

4

3 に答える 3

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を使用することもできます...

于 2013-08-13T05:52:19.827 に答える
0

この方法を試してください

String PATH = fileWithinMyDir.getAbsolutePath() + filename;
于 2012-07-19T07:39:18.090 に答える
0

私はそれを解決しました。次のコードを置き換えます

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

imageView.setImageDrawable(Drawable.createFromPath(img_path.get(i)));
于 2012-07-20T04:47:36.170 に答える