Android local file path cache method to get a summary of each

introduction

Daily development always encounter need to cache files locally, has always been a little confused on several ways to get the path of the corresponding cache path, the path of several methods available summary of what the corresponding

Several methods of obtaining path

Look at the test code and print the log

String CacheFilePath =getCacheDir().getAbsolutePath();
String ExternalCacheFilePath = getExternalCacheDir().getAbsolutePath();
String ExternalStorageFilePath =Environment.getExternalStorageDirectory().getAbsolutePath();

File CacheFile = new File(CacheFilePath,"CacheDir.txt");
File ExternalCacheFile = new File(ExternalCacheFilePath,"ExternalCacheDir.txt");
File ExternalStorageFile = new File(ExternalStorageFilePath,"ExternalStorage.txt");


  try {

        if (CacheFile.createNewFile()){
           Log.e("FileTest","CacheFile is created :" + CacheFile.getAbsolutePath());
        }else if (CacheFile.exists()){
           Log.e("FileTest","CacheFile is created :" + CacheFile.getAbsolutePath());
        }

        if (ExternalCacheFile.createNewFile()){
          Log.e("FileTest","ExternalCacheFile is created " + ExternalCacheFile.getAbsolutePath())
        }else if (ExternalCacheFile.exists()){
          Log.e("FileTest","ExternalCacheFile is created " + ExternalCacheFile.getAbsolutePath());
        }

        if (ExternalStorageFile.createNewFile()){ //NEED PERMISSION
          Log.e("FileTest","ExternalStorageFile is created :" + ExternalStorageFile.getAbsolutePath());
        }else if (ExternalStorageFile.exists()){
          Log.e("FileTest","ExternalStorageFile is created :" + ExternalStorageFile.getAbsolutePath());
        }
  } catch (IOException e) {
         e.printStackTrace();
  }

Print log

Here Insert Picture Description

analysis

Corresponding to the three methods:

        getCacheDir()
        getExternalCacheDir()
        Environment.getExternalStorageDirectory()
  • Belong to the internal memory: a first method
  • Belonging to the external storage: After two methods

The difference between the three directories

method category Competence path Remark
context.getCacheDir() Internal memory You do not need permission data / data / package name / cache Automatically deleted after uninstalling applications
context.getExternalCacheDir () External storage You do not need permission Android / data / package name / cache Automatically deleted after uninstalling applications
getExternalFilesDir(String type) External storage You do not need permission Android/data/包名/files If the incoming non-empty String will create the appropriate folder, such as incoming "test" is the Android / data / package name / files / test in its directory; automatically deleted after uninstalling applications
Environment.getExternalStorageDirectory () External storage Need permission SD card or an external storage root path After uninstalling the application still exists

And in the path of internal storage data / data we have on file manager on the phone can not see, in the lower right corner to pull over on the AS file management can be seen here
Here Insert Picture Description
Here Insert Picture Description

Several common conversion method

File 转 Uri

private static Uri getUriForFile(Context context, File file) {
        if (context == null || file == null) {//简单地拦截一下
            throw new NullPointerException();
        }
        Uri uri;
        if (Build.VERSION.SDK_INT >= 24) {
            uri = FileProvider.getUriForFile(context, "com.hjl.android7.fileprovider", file);
        } else {
            uri = Uri.fromFile(file);
        }
        return uri;
}

Uri 转 File

public File uri2File(Uri uri) { return new File(uri.getPath()); }

Turn Bitmap file path

public Bitmap File2Bitmap(String filePath){
    File file = new File(filePath);
    Uri uri = Uri.fromFile(file);
    Bitmap bitmap = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), uri);
}

Bitmap file transfer

public static boolean writeBitmapToFile(Bitmap bitmap, File file, int quality){
    	boolean flag = false;
		try {
			FileOutputStream fos = new FileOutputStream(file);
			flag = bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos);  
			fos.flush();
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
    }

Related

Adapter 7.0, Android open an album, camera, crop usage, details of the deal

Published 27 original articles · won praise 6 · views 1668

Guess you like

Origin blog.csdn.net/weixin_41802023/article/details/90693540