Save bitmap to app folder

前端 未结 5 1324
青春惊慌失措
青春惊慌失措 2020-12-15 02:03

When my app first starts, it lets the user select a profile picture. This can be done taking a photo at the moment, or selecting it from the gallery.

After the user

5条回答
  •  爱一瞬间的悲伤
    2020-12-15 02:37

    Try this ...

    You should use the Bitmap.compress() method to save a Bitmap as a file. It will compress (if the format used allows it) your picture and push it into an OutputStream.

    Here is an example of a Bitmap instance obtained through getImageBitmap(myurl) that can be compressed as a JPEG with a compression rate of 85% :

    String path = Environment.getExternalStorageDirectory().toString();
    OutputStream fOut = null;
    File file = new File(path, "FitnessGirl"+Contador+".jpg"); // the File to save to
    fOut = new FileOutputStream(file);
    
    Bitmap pictureBitmap = getImageBitmap(myurl); // obtaining the Bitmap
    pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
    fOut.flush();
    fOut.close(); // do not forget to close the stream
    
    MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
    

提交回复
热议问题