Android:当我在phone的内存中保存一个文件时,会存储那个文件在哪里?我怎样才能访问它的路径?

Android: When I save a file in the phone's internal memory will store Where's that file? How can I access it path?

我们有 2 个存储文件的记忆:

现在:

我可以使用 Environment.getExternalStorageDirectory ... 和 return 访问 sdcard 内存中的文件和根目录:/storage/sdcard0 这没问题。

但是:

当我在 phone 内存中存储文件和媒体(音乐,图片,...)时,如何获取 path phone 内存?

存储和检索应用程序数据的方式多种多样; SharedPreferences、内部存储、外部存储。 - SharedPreferences 基本上限制其他应用程序访问数据。 - 内部存储基本上将数据存储在 Phone 内存中 - 外部存储基本上将数据存储在可以安装或卸载的任何其他存储卡中。

如果我正确理解您的问题,那么您对外部存储感兴趣 - Public 文件,它基本上将文件存储在外部存储中,并允许任何其他应用程序/用户访问该文件。

例如: 文件样本 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);

对于应用程序的内部文件目录,使用

getFilesDir();

对于外部存储,使用

Environment.getExternalStorageDirectory();

好的,我通常使用这个助手class来保存我的媒体文件:

/**
 * 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;
    }
}

然后使用它我会做:

File currentFile= FileManager.getOutputMediaFile(2);

注意:媒体文件将在 sdcard/Pictures/AppName 中创建,但为了能够使用您的文件浏览器查看它们,请确保更新您的 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
        }
    });