How to store the photographed picture and obtain the uri of the picture for later processing
This includes external memory path acquisition, file storage, streaming, file compression and other technologies
public boolean saveImageToGallery(Bitmap bmp) { // Save the picture first String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "title"; File appDir = new File(storePath); if (!appDir.exists()) { appDir.mkdir(); } String fileName = System.currentTimeMillis() + ".jpg"; File file = new File(appDir, fileName); try { FileOutputStream fos = new FileOutputStream(file); //Compress and save pictures through io stream //Attention should be paid to image compression (quality compression is adopted here: it means that the memory remains unchanged, and the bytes.length after compression and conversion is reduced for transmission, but PNG will not affect it //Using JPEG format compression, the quality is 60. For a transparent picture (PNG), it will only lose transparency, and it will not affect a non transparent picture //Using PNG format, the compression quality is 50, which has no impact on PNG and JPEG images, but it will not reduce bytes Length, so JPEG is selected here //Note that when the mass is 100, it means no compression boolean isSuccess = bmp.compress(Bitmap.CompressFormat.JPEG, 60, fos); fos.flush(); fos.close(); //Insert the file into the system library MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), fileName, null); //After saving the picture, send a broadcast notification to update the database Uri uri = Uri.fromFile(file); sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri)); //System refresh album if (isSuccess) { return true; } else { return false; } } catch (IOException e) { e.printStackTrace(); } return false; }
It can be seen that the above code directly compresses and stores files in bitmap format. For details of obtaining bitmap files, see: About camera use in Android project (I) -- camera call_ Hersiman blog - CSDN blog
The following highlights the process of compressing files and storing them on devices
FileOutputStream fos = new FileOutputStream(file);
//Compress and save pictures through io stream
//Attention should be paid to image compression (quality compression is adopted here: it means that the memory remains unchanged, and the bytes.length after compression and conversion is reduced for transmission, but PNG will not affect it
//Using JPEG format compression, the quality is 60. For a transparent picture (PNG), it will only lose transparency, and it will not affect a non transparent picture
//Using PNG format, the compression quality is 50, which has no impact on PNG and JPEG images, but it will not reduce bytes Length, so JPEG is selected here
//Note that when the mass is 100, it means no compression
boolean isSuccess = bmp.compress(Bitmap.CompressFormat.JPEG, 60, fos);
fos.flush();
fos.close();
//Insert the file into the system library
MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), fileName, null);
//After saving the picture, send a broadcast notification to update the database
Uri uri = Uri.fromFile(file);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri)); // System refresh album
First, you need to open a so-called file output stream to put the bitmap file just obtained into the stream
Then compress the files in the stream, and the detailed compression information is in the comments
After the stream operation, the actual insertion is performed and the uri of the file stored here is obtained
Note: it is not necessary to notify the system to refresh the album here. If you need to find the picture on the device immediately and display it in the gallery instead of manually reopening the album, you can directly use the above / / code of the system to refresh the album. The parameter here is an intent variable