Good day. There is a question how to save the existing Bitmap in the phone's cache, and then have easy access to this file, within one application.

Download the picture in this way:

@Override protected Bitmap doInBackground(Void... params) { try { URL urlConnection = new URL(url); HttpURLConnection connection = (HttpURLConnection) urlConnection .openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); Bitmap myBitmap = BitmapFactory.decodeStream(input); return myBitmap; } catch (Exception e) { e.printStackTrace(); } return null; } 

Then use for their own purposes. But how to save and use it in the future, without having access to the network?

    1 answer 1

    After downloading, save it to a file:

     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes); //Задаете путь и имя File file = new File(Environment.getExternalStorageDirectory() + File.separator + "myBitmap.jpg") file.createNewFile(); //Запись FileOutputStream fileOutputStream = new FileOutputStream(f); fileOutputStream.write(bytes.toByteArray()); fileOutputStream.close(); 

    Do not forget to wrap in try-catch and the necessary write permissions: "android.permission.WRITE_EXTERNAL_STORAGE"

    Then read from the file and drive in Bitmap :

     Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapData , 0, bitmapData.length);