Preservation problem



  • I'm trying to keep the picture, nothing's going on. Two methods used to maintain and download

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

    I keep the picture here 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;
    

    I'm down here. OnCreate method

    loadImageFromStorage("/data/data/yourapp/app_data/imageDir");



  • Try this code, I'm working!

     /**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;
      }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2