android.os.Environment.getDataDirectory()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(711)

本文整理了Java中android.os.Environment.getDataDirectory()方法的一些代码示例,展示了Environment.getDataDirectory()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Environment.getDataDirectory()方法的具体详情如下:
包路径:android.os.Environment
类名称:Environment
方法名:getDataDirectory

Environment.getDataDirectory介绍

暂无

代码示例

代码示例来源:origin: zaaach/CityPicker

  1. public DBManager(Context context) {
  2. this.mContext = context;
  3. DB_PATH = File.separator + "data"
  4. + Environment.getDataDirectory().getAbsolutePath() + File.separator
  5. + context.getPackageName() + File.separator + "databases" + File.separator;
  6. copyDBFile();
  7. }

代码示例来源:origin: JZ-Darkal/AndroidHttpCapture

  1. /**
  2. * 获取外部存储目录,一般是/sdcard/ 如果不存在,就返回/data/
  3. */
  4. public static File getDataRoot() {
  5. try {
  6. if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ||
  7. (Environment.getExternalStorageDirectory().exists() && Environment.getExternalStorageDirectory().canWrite())) {
  8. return Environment.getExternalStorageDirectory();
  9. } else {
  10. return Environment.getDataDirectory();
  11. }
  12. } catch (Exception e) {
  13. return null;
  14. }
  15. }

代码示例来源:origin: TommyLemon/APIJSON

  1. private static CityDB openCityDB(Context context, String packageName) {
  2. String path = "/data"
  3. + Environment.getDataDirectory().getAbsolutePath()
  4. + File.separator + packageName + File.separator
  5. + CityDB.CITY_DB_NAME;
  6. File db = new File(path);
  7. if (!db.exists()) {
  8. try {
  9. InputStream is = context.getAssets().open("city.db");
  10. FileOutputStream fos = new FileOutputStream(db);
  11. int len = -1;
  12. byte[] buffer = new byte[1024];
  13. while ((len = is.read(buffer)) != -1) {
  14. fos.write(buffer, 0, len);
  15. fos.flush();
  16. }
  17. fos.close();
  18. is.close();
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. System.exit(0);
  22. }
  23. }
  24. return new CityDB(context, path);
  25. }

代码示例来源:origin: jgilfelt/chuck

  1. private static String extractDatabase(Context context) {
  2. try {
  3. File external = context.getExternalFilesDir(null);
  4. File data = Environment.getDataDirectory();
  5. if (external != null && external.canWrite()) {
  6. String dataDBPath = "data/" + context.getPackageName() + "/databases/chuck.db";
  7. String extractDBPath = "chuckdb.temp";
  8. File dataDB = new File(data, dataDBPath);
  9. File extractDB = new File(external, extractDBPath);
  10. if (dataDB.exists()) {
  11. FileChannel in = new FileInputStream(dataDB).getChannel();
  12. FileChannel out = new FileOutputStream(extractDB).getChannel();
  13. out.transferFrom(in, 0, in.size());
  14. in.close();
  15. out.close();
  16. return extractDB.getAbsolutePath();
  17. }
  18. }
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. return null;
  23. }

代码示例来源:origin: TommyLemon/Android-ZBLibrary

  1. private static CityDB openCityDB(Context context, String packageName) {
  2. String path = "/data"
  3. + Environment.getDataDirectory().getAbsolutePath()
  4. + File.separator + packageName + File.separator
  5. + CityDB.CITY_DB_NAME;
  6. File db = new File(path);
  7. if (!db.exists()) {
  8. try {
  9. InputStream is = context.getAssets().open("city.db");
  10. FileOutputStream fos = new FileOutputStream(db);
  11. int len = -1;
  12. byte[] buffer = new byte[1024];
  13. while ((len = is.read(buffer)) != -1) {
  14. fos.write(buffer, 0, len);
  15. fos.flush();
  16. }
  17. fos.close();
  18. is.close();
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. System.exit(0);
  22. }
  23. }
  24. return new CityDB(context, path);
  25. }

代码示例来源:origin: jeasonlzy/ImagePicker

  1. if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
  2. if (Utils.existSDCard()) takeImageFile = new File(Environment.getExternalStorageDirectory(), "/DCIM/camera/");
  3. else takeImageFile = Environment.getDataDirectory();
  4. takeImageFile = createFile(takeImageFile, "IMG_", ".jpg");
  5. if (takeImageFile != null) {

代码示例来源:origin: joyoyao/superCleanMaster

  1. @Override
  2. protected Long doInBackground(Void... params) {
  3. final CountDownLatch countDownLatch = new CountDownLatch(1);
  4. StatFs stat = new StatFs(Environment.getDataDirectory().getAbsolutePath());
  5. try {
  6. mFreeStorageAndNotifyMethod.invoke(getPackageManager(),
  7. (long) stat.getBlockCount() * (long) stat.getBlockSize(),
  8. new IPackageDataObserver.Stub() {
  9. @Override
  10. public void onRemoveCompleted(String packageName, boolean succeeded)
  11. throws RemoteException {
  12. countDownLatch.countDown();
  13. }
  14. }
  15. );
  16. countDownLatch.await();
  17. } catch (InvocationTargetException | InterruptedException | IllegalAccessException e) {
  18. e.printStackTrace();
  19. }
  20. return mCacheSize;
  21. }

代码示例来源:origin: HotBitmapGG/bilibili-android-client

  1. /**
  2. * 获取手机内部存储总空间
  3. */
  4. public static long getPhoneTotalSize() {
  5. if (!checkSdCard()) {
  6. File path = Environment.getDataDirectory();
  7. StatFs mStatFs = new StatFs(path.getPath());
  8. long blockSizeLong = mStatFs.getBlockSizeLong();
  9. long blockCountLong = mStatFs.getBlockCountLong();
  10. return blockSizeLong * blockCountLong;
  11. } else {
  12. return getSDcardTotalSize();
  13. }
  14. }

代码示例来源:origin: Rukey7/MvpApp

  1. @Deprecated
  2. public static boolean checkRomSpaceEnough(long limitSize) {
  3. long allSize;
  4. long availableSize = 0;
  5. try {
  6. File data = Environment.getDataDirectory();
  7. StatFs sf = new StatFs(data.getPath());
  8. availableSize = (long) sf.getAvailableBlocks() * (long) sf.getBlockSize();
  9. allSize = (long) sf.getBlockCount() * (long) sf.getBlockSize();
  10. } catch (Exception e) {
  11. allSize = 0;
  12. }
  13. if (allSize != 0 && availableSize > limitSize) {
  14. return true;
  15. }
  16. return false;
  17. }

代码示例来源:origin: HotBitmapGG/bilibili-android-client

  1. /**
  2. * 获取手机内存存储可用空间
  3. */
  4. public static long getPhoneAvailableSize() {
  5. if (!checkSdCard()) {
  6. File path = Environment.getDataDirectory();
  7. StatFs mStatFs = new StatFs(path.getPath());
  8. long blockSizeLong = mStatFs.getBlockSizeLong();
  9. long availableBlocksLong = mStatFs.getAvailableBlocksLong();
  10. return blockSizeLong * availableBlocksLong;
  11. } else
  12. return getSDcardAvailableSize();
  13. }
  14. }

代码示例来源:origin: joyoyao/superCleanMaster

  1. public static SDCardInfo getSystemSpaceInfo(Context context) {
  2. File path = Environment.getDataDirectory();
  3. // File path = context.getCacheDir().getAbsoluteFile();
  4. StatFs stat = new StatFs(path.getPath());
  5. long blockSize = stat.getBlockSize();
  6. long totalBlocks = stat.getBlockCount();
  7. long availableBlocks = stat.getAvailableBlocks();
  8. long totalSize = blockSize * totalBlocks;
  9. long availSize = availableBlocks * blockSize;
  10. SDCardInfo info = new SDCardInfo();
  11. info.total = totalSize;
  12. info.free = availSize;
  13. return info;
  14. }

代码示例来源:origin: ACRA/acra

  1. /**
  2. * Calculates the total memory of the device. This is based on an inspection of the filesystem, which in android devices is stored in RAM.
  3. *
  4. * @return Total number of bytes.
  5. */
  6. private long getTotalInternalMemorySize() {
  7. final File path = Environment.getDataDirectory();
  8. final StatFs stat = new StatFs(path.getPath());
  9. final long blockSize;
  10. final long totalBlocks;
  11. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
  12. blockSize = stat.getBlockSizeLong();
  13. totalBlocks = stat.getBlockCountLong();
  14. } else {
  15. //noinspection deprecation
  16. blockSize = stat.getBlockSize();
  17. //noinspection deprecation
  18. totalBlocks = stat.getBlockCount();
  19. }
  20. return totalBlocks * blockSize;
  21. }

代码示例来源:origin: ACRA/acra

  1. /**
  2. * Calculates the free memory of the device. This is based on an inspection of the filesystem, which in android
  3. * devices is stored in RAM.
  4. *
  5. * @return Number of bytes available.
  6. */
  7. private long getAvailableInternalMemorySize() {
  8. final File path = Environment.getDataDirectory();
  9. final StatFs stat = new StatFs(path.getPath());
  10. final long blockSize;
  11. final long availableBlocks;
  12. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
  13. blockSize = stat.getBlockSizeLong();
  14. availableBlocks = stat.getAvailableBlocksLong();
  15. } else {
  16. //noinspection deprecation
  17. blockSize = stat.getBlockSize();
  18. //noinspection deprecation
  19. availableBlocks = stat.getAvailableBlocks();
  20. }
  21. return availableBlocks * blockSize;
  22. }

代码示例来源:origin: kingthy/TVRemoteIME

  1. public static String getRootFilePath() {
  2. if (hasSDCard()) {
  3. return Environment.getExternalStorageDirectory().getAbsolutePath() + "/";// filePath:/sdcard/
  4. } else {
  5. return Environment.getDataDirectory().getAbsolutePath() + "/data/"; // filePath:
  6. // /data/data/
  7. }
  8. }

代码示例来源:origin: jingle1267/android-utils

  1. String sourceFilePath = Environment.getDataDirectory() + "/data/"
  2. + context.getPackageName() + "/databases/" + databaseName;
  3. String destFilePath = Environment.getExternalStorageDirectory()

代码示例来源:origin: smuyyh/SprintNBA

  1. /**
  2. * 获取Android数据目录
  3. *
  4. * @return
  5. */
  6. public static String getDataPath() {
  7. return Environment.getDataDirectory().getPath();
  8. }

代码示例来源:origin: ittianyu/MobileGuard

  1. /**
  2. * get the rom free space
  3. *
  4. * @return the byte of free space
  5. */
  6. public static long getRomFreeSpace() {
  7. File directory = Environment.getDataDirectory();
  8. return directory.getFreeSpace();
  9. }

代码示例来源:origin: Odoo-mobile/framework

  1. public String databaseLocalPath() {
  2. App app = (App) mContext.getApplicationContext();
  3. return Environment.getDataDirectory().getPath() +
  4. "/data/" + app.getPackageName() + "/databases/" + getDatabaseName();
  5. }

代码示例来源:origin: marzika/Snapprefs

  1. @SuppressWarnings("deprecation")
  2. public void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. getPreferenceManager().setSharedPreferencesMode(Context.MODE_WORLD_READABLE);
  5. addPreferencesFromResource(preferenceId);
  6. File prefsFile = new File(
  7. Environment.getDataDirectory(), "data/"
  8. + BaseSettings.class.getPackage().getName() + "/shared_prefs/" + BaseSettings.class.getPackage().getName()
  9. + "_preferences" + ".xml");
  10. if( prefsFile.exists())
  11. prefsFile.setReadable(true, false);
  12. }

代码示例来源:origin: ManbangGroup/Phantom

  1. /**
  2. * 上报内部存储可用的磁盘空间
  3. */
  4. public static void reportUsableSpaceMegabytes() {
  5. final long usableSpaceMegabytes = Environment.getDataDirectory().getUsableSpace() / 1024 / 1024;
  6. reportLog("usableSpace MB: " + usableSpaceMegabytes);
  7. }

相关文章