we have 2 memories for store files:

  • 1- phone memory
  • 2- sd card memory

now :

I can access to files and roots in sdcard memory with Environment.getExternalStorageDirectory ... and return : /storage/sdcard0 this is ok.

but :

When i store files and media (music,pic, ...) in phone memory, how can to get path phone memory?

3

There are 3 answers

0
ganeshvjy On BEST ANSWER

There are different ways of storing and retrieving application data; SharedPreferences, Internal Storage, External Storage. - SharedPreferences basically restricts other apps from accessing the data. - Internal Storage basically stores the data in Phone memory - External Storage basically stores the data in any other memory cards which can be mounted or unmounted.

If I understand your question properly, then you are interested in External Storage - Public files which basically stores the files in External storage and allows any other apps / user to access the file.

For example: File sample = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);

0
alijandro On

For app's internal file directory, use

getFilesDir();

For external storage, use

Environment.getExternalStorageDirectory();
0
Fabrice Benimana On

Okay, I usually use this helper class to save my media files:

/**
 * This class create, name given the timestamp and save a multimedia file in the Environment.DIRECTORY_PICTURES folder
 * This location works best if you want the created images to be shared
 * between applications and persist after your app has been uninstalled.
 */
public class FileManager {

    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;


    public static Uri getOutputMediaFileUri(int type){
        return Uri.fromFile(getOutputMediaFile(type));
    }

    /** Create a File for saving an image or video */
   public static File getOutputMediaFile(int type){


        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), VideoRecordingActivity.TAG);



        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE){
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_"+ timeStamp + ".jpg");
        } else if(type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "VID_"+ timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }
}

Then to use it I will just do:

File currentFile= FileManager.getOutputMediaFile(2);

Note: The media file will be created in sdcard/Pictures/AppName but to be able to see them with your file browser make sure to update your mediaScanner:

MediaScannerConnection.scanFile(getApplicationContext(), new String[]{file.getPath()}, null,
    new MediaScannerConnection.OnScanCompletedListener() {
        @Override
        public void onScanCompleted(String path, final Uri fileUri) {           
            //Eventually some UI updates
        }
    });