update android image gallery with newly created bitmap

前端 未结 4 1480
温柔的废话
温柔的废话 2020-12-17 06:20

I\'m trying to save an image file to external storage. I can save the picture to the sdcard but it doesn\'t show up in Androids gallery application. I\'ve tried this approac

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-17 06:49

    Use this code to save an image Bitmap in android device gallery

    public void savePhoto(Bitmap bmp)
    {
    imageFileFolder = new File(Environment.getExternalStorageDirectory(),"Rotate");
    imageFileFolder.mkdir();
    FileOutputStream out = null;
    Calendar c = Calendar.getInstance();
    String date = fromInt(c.get(Calendar.MONTH))
                + fromInt(c.get(Calendar.DAY_OF_MONTH))
                + fromInt(c.get(Calendar.YEAR))
                + fromInt(c.get(Calendar.HOUR_OF_DAY))
                + fromInt(c.get(Calendar.MINUTE))
                + fromInt(c.get(Calendar.SECOND));
    imageFileName = new File(imageFileFolder, date.toString() + ".jpg");
    try
    {
     out = new FileOutputStream(imageFileName);
     bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
     out.flush();
     out.close();
     scanPhoto(imageFileName.toString());
     out = null;
    } catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    
    
    public String fromInt(int val)
    {
    return String.valueOf(val);
    }
    
    
    public void scanPhoto(final String imageFileName)
    {
    msConn = new MediaScannerConnection(PreviewDemo1.this,new MediaScannerConnectionClient()
    {
    public void onMediaScannerConnected()
    {
    msConn.scanFile(imageFileName, null);
    Log.i("msClient obj  in Photo Utility","connection established");
    }
    public void onScanCompleted(String path, Uri uri)
    {
    msConn.disconnect();
    Log.i("msClient obj in Photo Utility","scan completed");
    }
    });
    msConn.connect();
    } 
    

    Here i am saving the image in " Rotate " folder if you dont want that you can change it easily in savePhoto method.

提交回复
热议问题