Today when testing the Android camera feature encountered a confusion: the photo shoot, the program can be read, but in the cell phone storage can not find the photos taken. The process of learning through the twists and turns are recorded as follows:
One: Take pictures and keep
Save the image in the callback interface by calling the Android camera interface, taking the photo:
Public voidOnpicturetaken (byte[] bytes, camera camera) {//Define file nameDate Date =NewDate (); SimpleDateFormat DateFormat=NewSimpleDateFormat ("Yyyymmddhhmmssss"); String FileName= Dateformat.format (date) + ". jpg"; FileOutputStream OS=NULL; BooleanSuccess =true; Try{//Save to internal storageOS =getactivity (). Openfileoutput (path, context.mode_private); Os.write (bytes); }Catch(IOException e) {Success=false; LOG.E (TAG,"Error writing to file" +FileName, E);}
The original call: Getactivity (). Openfileoutput (Path, context.mode_private), the file is stored in the app's private storage in the/data/data/...app package path/file, This private directory cannot be accessed externally, so it must not be visible in the phone's storage.
Second: storage to external storage
Public voidOnpicturetaken (byte[] bytes, camera camera) { //Define file nameDate Date =NewDate (); SimpleDateFormat DateFormat=NewSimpleDateFormat ("Yyyymmddhhmmssss"); String FileName= Dateformat.format (date) + ". jpg"; FileOutputStream OS=NULL; BooleanSuccess =true; Try{File path=environment. Getexternalstoragepublicdirectory (ENVIRONMENT.DIRECTORY_DCIM); File File=NewFile (path, fileName); Path.mkdirs (); if(Path.exists ()) {OS=Newfileoutputstream (file); }Else{log.e (TAG,"Create Path Failed"); return; } //Save to internal storageos.write (bytes); }Catch(IOException e) {Success=false; LOG.E (TAG,"Error writing to file" +FileName, E); }
To store read and write files externally, request permission to add the following permissions in the app configuration file:
<uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name = "Android.permission.WRITE_EXTERNAL_STORAGE"/>
Well, the file was saved successfully, but it's still not visible in the cell phone store, why?
Originally, the Android store added files, and can not refresh the storage directory in a timely manner, need to restart the phone or manually refresh the directory through the code. Manually refreshing the catalog has been tested by me in the following two ways:
1. Send the broadcast
Sendbroadcast (New Intent (Intent.action_media_scanner_scan_file, Uri.parse ("file://" + File.getabsolutepath (). ToString ()));
The second parameter refers to the absolute path where the file is saved
2.MediaScannerConnection
Mediascannerconnection.scanfile (Getactivity (), New String[]{file.getabsolutepath (). toString ()}, NULL, NULL);
Three: summary
Android storage is divided into app private storage and external storage. App private storage is not accessible to other apps. External storage can be shared by other apps. External storage does not refer to SD card storage, but also to the phone's own storage space.
About Android Storage