2016-10-30 9 views
0

URLから画像を読み込んでアンドロイドのデバイスのメモリに保存するにはどうすればいいですか?私はピカソまたはoserの図書館を使用しないでください。 私は必要があります: デバイスがインターネット接続を取得する場合、ImageViewにURLからイメージをロードし、デバイスのメモリに保存します。そうでなければ、imageViewにイメージを保存する必要があります。助けてくれてありがとう。URLから画像を読み込んでアンドロイドのメモリに保存します

P.S.申し訳ありませんが、私は英語をよく知っていないので、間違いがあります。 この私のクラス:

public class ImageManager { 
    String file_path; 
    Bitmap bitmap = null; 
    public Bitmap donwoaledImageFromSD() { 

     File image = new File(Environment.getExternalStorageDirectory().getPath(),file_path); 
     BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
     bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions); 

     return bitmap; 
    } 

    private void savebitmap() { 
     File file = new File("first"); 
     file_path = file.getAbsolutePath(); 
     try { 
      FileOutputStream fileOutputStream = new FileOutputStream(file); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 90,fileOutputStream); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void fetchImage(final String url, final ImageView iView) { 
     new AsyncTask<String, Void, Bitmap>() { 
      protected Bitmap doInBackground(String... iUrl) { 

       try { 
        InputStream in = new URL(url).openStream(); 
        bitmap = BitmapFactory.decodeStream(in); 
        savebitmap(); 

       } catch (Exception e) { 
        donwoaledImageFromSD(); 
       } 
       return bitmap; 
      } 

      protected void onPostExecute(Bitmap result) { 
       super.onPostExecute(result); 
       if (iView != null) { 
        iView.setImageBitmap(result); 
       } 
      } 
     }.execute(url); 
    } 
} 
+1

Y uはグライドのような信頼性の高いライブラリを使用していけない:あなたはgetBitmapFromURL()を呼び出す必要がありますので

アップデート2

方法getBitmapFromURL()が、ImageViewは、例えば、このよう、UIスレッドから更新されなければなりません、Picasoなど –

+0

スタンドアートライブラリー(グライド、ピカソなど)を使用しない基準の1つです –

+0

あなたが試したことと遭遇した具体的な問題を示す[mcve]を提供してください。 – CommonsWare

答えて

0

てみてくださいこのコードを使用する:imageUrl

public Bitmap getBitmapFromURL(String imageUrl) { 
     try { 
      URL url = new URL(imageUrl); 
      HttpURLConnection connection = (HttpURLConnection) 

    url.openConnection(); 
       connection.setDoInput(true); 
       connection.connect(); 
       InputStream inputStream = connection.getInputStream(); 
       Bitmap imageBitmap = BitmapFactory.decodeStream(inputStream); 
       return imageBitmap; 
      } catch (IOException e) { 
       e.printStackTrace(); 
       return null; 
      } 
     } 

からロード画像の

方法、あなたはそのように、別のスレッドでそれを使用する必要があります。

new Thread(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        Bitmap bitmap = getBitmapFromURL(<URL of your image>); 
        imageView.setImageBitmap(bitmap); 

       } catch (Exception e) { 

        e.printStackTrace(); 
        e.getMessage(); 
       } 

      } 
     }).start(); 

しかし、実際にはピカソを使用してください。

更新:

外部ストレージ上のファイルにBitmapを保存する(SDカード)あなたはこのようなメソッドを使用することができます:

public static void writeBitmapToSD(String aFileName, Bitmap aBitmap) { 

      if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 
       return; 
      } 

      File sdPath = Environment.getExternalStorageDirectory(); 
      File sdFile = new File(sdPath, aFileName); 

      if (sdFile.exists()) { 
       sdFile.delete(); 
      } 

      try { 
       FileOutputStream out = new FileOutputStream(sdFile); 
       aBitmap.compress(Bitmap.CompressFormat.PNG, 100, out); 
       out.flush(); 
       out.close(); 
      } catch (Exception e) { 

      } 

     } 

は、あなたがのために

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

が必要であることを忘れないでくださいそれ。

And for loading `Bitmap` from file on external storage You can use method like that: 

public static Bitmap loadImageFromSD(String aFileName) { 
     Bitmap result = null; 
     if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 
      try { 
       FileInputStream fis = new FileInputStream(new File(Environment.getExternalStorageDirectory(), aFileName)); 
       result = BitmapFactory.decodeStream(fis); 
       fis.close(); 
      } catch (FileNotFoundException e) { 
       Log.d(TAG, "loadImageFromSD: " + e.getMessage()); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return result; 
    } 

これを行うには、

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 

を必要としています。

new Thread(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        final Bitmap bitmap = getBitmapFromURL("<your_image_URL>"); 

        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          imageView.setImageBitmap(bitmap); 
         } 
        }); 


       } catch (Exception e) { 

        e.printStackTrace(); 
        e.getMessage(); 
       } 

      } 
     }).start(); 
+0

答えをありがとうが、ビットマップを保存してimageViewに貼り付ける必要があります。また、私はピカソがより良い方法であることを知っています。しかし、それはスタンダードのライブラリー(グライド、ピカソなど)を使わないという基準の一つです。 –

+0

申し訳ありませんが、URLから画像を読み込んでいます。 –

+0

回答の更新2をご覧ください。 –

関連する問題