About Android Storage

Source: Internet
Author: User
Tags dateformat

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.