从 URL 加载图像并保存在 android 中的内存中

load image from URL and save on memory in android

如何从 URL 加载图像并将其保存在 android 的设备内存中?别说我用 Picasso 或 oser laibrary。 我需要: 如果设备获得互联网连接,我将图像从 url 加载到 ImageView 并将其保存在设备的内存中,否则我需要将保存图像之一加载到 imageView。感谢您的帮助

P.S。对不起,我可能会犯一些错误,因为我不太懂英语。 这是我的 class:

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

尝试使用此代码:

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

但使用 Picasso - 确实是更好的方法。

更新:

要将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"/>

做到这一点。

更新 2

方法getBitmapFromURL(),但是ImageView应该从UI线程更新,所以你应该调用getBitmapFromURL(),例如,这样:

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