I try to save the picture, nothing comes out. I use two methods for saving and loading.

private String saveToInternalStorage(Bitmap bitmapImage) throws IOException { ContextWrapper cw = new ContextWrapper(getApplicationContext()); // путь /data/data/yourapp/app_data/imageDir File directory = cw.getDir("imageDir", Context.MODE_PRIVATE); // Создаем imageDir File mypath=new File(directory,"user.png"); FileOutputStream fos = null; try { fos = new FileOutputStream(mypath); // Используем метод сжатия BitMap объекта для записи в OutputStream bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos); } catch (Exception e) { e.printStackTrace(); } finally { fos.close(); } return directory.getAbsolutePath(); } private void loadImageFromStorage(String path) { try { Toast.makeText(this, "Загружено", Toast.LENGTH_LONG).show(); File f=new File(path, "user.png"); Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f)); channelLogo.setImageBitmap(b); } catch (FileNotFoundException e) { e.printStackTrace(); } } 

Here I save the picture by pressing the menu button

 case R.id.save: Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.user); try { saveToInternalStorage(icon); Toast.makeText(this, "Сохранено", Toast.LENGTH_LONG).show(); } catch (IOException e) { e.printStackTrace(); } return true; 

Here I upload. Method located in the method OnCreate

 loadImageFromStorage("/data/data/yourapp/app_data/imageDir"); 
  • 2
    And the error is ...? - iFr0z
  • @YuraIvanov afraid, without you, we can not cope. - iFr0z
  • / data / data / yourapp / app_data / imageDir is like an example. Instead of your_app - the name of the software package - Ziens
  • in loadImageFromStorage you need to substitute what was returned by the saveToInternalStorage method - Ziens

2 answers 2

Try this code, it works for me!

  /**Method for save bitmap object * to file directory on device * * @param fileName file name need concat format: fileName + ".png" * @param bitmap * @return true if object be saved */ public boolean saveImageToFile(String fileName, Bitmap bitmap) { File imageFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "folderName" ,fileName); Bitmap.CompressFormat format = Bitmap.CompressFormat.PNG; int quality = 100; FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(imageFile); bitmap.compress(format, quality, fileOutputStream); fileOutputStream.close(); return true; } catch (IOException e) { Log.e("app",e.getMessage()); if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e1) { e1.printStackTrace(); } } } return false; } /**Method for get user bitmap * from file directory on device * * @param fileName file name need format: "fileName" + ".png" * @return drawable */ public Drawable getImageFromFile(String fileName) { Drawable drawable; String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "folderName" + File.separator + fileName; BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; Bitmap photo = BitmapFactory.decodeFile(path, options); drawable = new BitmapDrawable(photo); return drawable; } 
     private void saveToInternalStorage(Bitmap bitmapImage) throws IOException { ContextWrapper cw = new ContextWrapper(getApplicationContext()); // путь /data/data/yourapp/app_data/imageDir File directory = cw.getDir("imageDir", Context.MODE_PRIVATE); // Создаем imageDir File mypath = new File(directory, "user.png"); FileOutputStream fos = null; try { fos = new FileOutputStream(mypath); // Используем метод сжатия BitMap объекта для записи в OutputStream bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos); } catch (Exception e) { e.printStackTrace(); } finally { fos.close(); } SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); prefs.edit().putString("image_path", directory.getAbsolutePath()).commit(); } private void loadImageFromStorage() { try { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); String path = prefs.getString("image_path", ""); Toast.makeText(this, "Загружено", Toast.LENGTH_LONG).show(); File f = new File(path, "user.png"); Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f)); channelLogo.setImageBitmap(b); } catch (FileNotFoundException e) { e.printStackTrace(); } } 

    Everything is working