Java Code Examples for android.os.Environment.getExternalStorageDirectory()

The following are Jave code examples for showing how to use getExternalStorageDirectory() of the android.os.Environment class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: GitHub   File: StatFsHelper.java   View Source Code Vote up 8 votes
/**
 * Initialization code that can sometimes take a long time.
 */
private void ensureInitialized() {
  if (!mInitialized) {
   lock.lock();
    try {
      if (!mInitialized) {
        mInternalPath = Environment.getDataDirectory();
        mExternalPath = Environment.getExternalStorageDirectory();
        updateStats();
        mInitialized = true;
      }
    } finally {
      lock.unlock();
    }
  }
}
 
Example 2
Project: hybrid   File: SyncVersion.java   View Source Code Vote up 7 votes
private void saveMarkIntoFile(byte[] version) {
    if (version!=null){
        if (FileUtils.isSdcardExist()) {
            String sdPath = Environment.getExternalStorageDirectory() + "/";
            String mSavePath = sdPath + Config.DIRECTORY;
            FileUtils.createDirFile(mSavePath);
            File txtFile = FileUtils.createNewFile(mSavePath + "/" + Config.VERSION_FILE);//创建文件
            if (txtFile!=null){
                try {
                    FileOutputStream fos = new FileOutputStream(txtFile);
                    fos.write(version);
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
 
Example 3
Project: GifImageLoader   File: StorageUtils.java   View Source Code Vote up 6 votes
private static File getExternalCacheDir(Context context) {
	File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data");
	File appCacheDir = new File(new File(dataDir, context.getPackageName()), "cache");
	if (!appCacheDir.exists()) {
		if (!appCacheDir.mkdirs()) {
			L.w("Unable to create external cache directory");
			return null;
		}
		try {
			new File(appCacheDir, ".nomedia").createNewFile();
		} catch (IOException e) {
			L.i("Can't create \".nomedia\" file in application external cache directory");
		}
	}
	return appCacheDir;
}
 
Example 4
Project: buildAPKsApps   File: BloodTable.java   View Source Code Vote up 6 votes
@Override
   public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
		case android.R.id.home: 
			onBackPressed();
			break;
		case R.id.action_save:
			new SaveImageTask().execute(null, null, null);
			break;
		case R.id.action_share:
			Intent share = new Intent(Intent.ACTION_SEND);
			share.setType("image/png");
	        File folder = new File(Environment.getExternalStorageDirectory() + "/Blood Type");
	        file = new File(folder + "/chart.png");
	        if (!folder.exists()) {
	            folder.mkdir();
	        }
			try {
				if (file.exists()) {
					share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
					startActivityForResult(Intent.createChooser(share, "Share Blood Chart"), 1);
				}
				else {
					new SaveImageTask().execute(null, null, null);
					share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
					startActivityForResult(Intent.createChooser(share, "Share Blood Chart"), 1);
				}
			}
			catch (Exception e) {
			    e.printStackTrace();
			}
	        return true;
		default:
			return super.onOptionsItemSelected(item);
	}
	return true;
}
 
Example 5
Project: cleanhood   File: CreateEventActivity.java   View Source Code Vote up 6 votes
public void accessPic(){
    System.out.println("ACCESSPIC called");
    photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
    Uri selectedImage = Uri.fromFile(photo);

    getContentResolver().notifyChange(selectedImage, null);

    ContentResolver cr = getContentResolver();
    try {
        bitmap = android.provider.MediaStore.Images.Media
                .getBitmap(cr, selectedImage);

        mImageView.setImageBitmap(bitmap);

    } catch (Exception e) {
        Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                .show();
        Log.e("Camera", e.toString());
    }
}
 
Example 6
Project: Watermark   File: StorageUtils.java   View Source Code Vote up 6 votes
private static File getExternalCacheDir(Context context) {
    File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data");
    File appCacheDir = new File(new File(dataDir, context.getPackageName()), "cache");
    if (!appCacheDir.exists()) {
        if (!appCacheDir.mkdirs()) {
            Logger.w("Unable to create external cache directory");
            return null;
        }
        try {
            new File(appCacheDir, ".nomedia").createNewFile();
        } catch (IOException e) {
            Logger.i("Can't create \".nomedia\" file in application external cache directory");
        }
    }
    return appCacheDir;
}
 
Example 7
Project: GroupingMessages   File: DatabaseBridge.java   View Source Code Vote up 6 votes
static Boolean exportDB(Context context) {

        unInitializeDb();

        try {
            File sd = Environment.getExternalStorageDirectory();

            if (sd.canWrite()) {
                File currentDB = context.getDatabasePath(DatabaseContract.DATABASE_NAME);
                File backupDB = new File(sd, BACKUP_DB_PATH);

                try (FileInputStream fis = new FileInputStream(currentDB)) {
                    try (FileOutputStream fos = new FileOutputStream(backupDB)) {
                        FileChannel src = fis.getChannel();
                        FileChannel dst = fos.getChannel();
                        dst.transferFrom(src, 0, src.size());
                        src.close();
                        dst.close();
                        fis.close();
                        fos.close();
                    }
                }
                initializeDb(context);
                return true;
            }
        } catch (Exception e) {
            Log.e("GM/exportDb", e.toString());
        }
        initializeDb(context);
        return false;
    }
 
Example 8
Project: Pluto-Android   File: FileUtils.java   View Source Code Vote up 6 votes
/**
 * 计算SD卡的剩余空间
 * @return 返回-1,说明没有安装sd卡
 */
public static long getFreeDiskSpace() {
	String status = Environment.getExternalStorageState();
	long freeSpace = 0;
	if (status.equals(Environment.MEDIA_MOUNTED)) {
		try {
			File path = Environment.getExternalStorageDirectory();
			StatFs stat = new StatFs(path.getPath());
			long blockSize = stat.getBlockSize();
			long availableBlocks = stat.getAvailableBlocks();
			freeSpace = availableBlocks * blockSize / 1024;
		} catch (Exception e) {
			e.printStackTrace();
		}
	} else {
		return -1;
	}
	return (freeSpace);
}
 
Example 9
Project: starcraft-2-build-player   File: JsonBuildService.java   View Source Code Vote up 5 votes
/**
 * Creates builds directory on user's SD card if needed
 *
 * @return false if the builds dir doesn't exist and couldn't be created,
 * true otherwise
 */
public static boolean createBuildsDirectory() {
    File root = Environment.getExternalStorageDirectory();
    File buildsDir = new File(root, BUILDS_DIR);
    if (!buildsDir.exists()) {
        return buildsDir.mkdirs();
    }
    return true;
}
 
Example 10
Project: Orin   File: MusicUtil.java   View Source Code Vote up 5 votes
@NonNull
@SuppressWarnings("ResultOfMethodCallIgnored")
public static File createAlbumArtDir() {
    File albumArtDir = new File(Environment.getExternalStorageDirectory(), "/albumthumbs/");
    if (!albumArtDir.exists()) {
        albumArtDir.mkdirs();
        try {
            new File(albumArtDir, ".nomedia").createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return albumArtDir;
}
 
Example 11
Project: letv   File: Util.java   View Source Code Vote up 5 votes
public static boolean hasSDCard() {
    File file = null;
    if (Environment.getExternalStorageState().equals("mounted")) {
        file = Environment.getExternalStorageDirectory();
    }
    if (file != null) {
        return true;
    }
    return false;
}
 
Example 12
Project: cordova-logcat-filelogger   File: LogCatPlugin.java   View Source Code Vote up 5 votes
@Override
public void initialize(final CordovaInterface cordova, final CordovaWebView webView) {
    Log.v(TAG, "Init");
    this.webView = super.webView;
    this.cordovaInstance = super.cordova;
    this.context = cordovaInstance.getActivity().getApplicationContext();
    this.internalStorage = this.context.getFilesDir();
    this.externalStorage = new File(Environment.getExternalStorageDirectory() +
            File.separator + EXTERNAL_STORAGE_FOLDER);
}
 
Example 13
Project: boohee_v5.6   File: SDcard.java   View Source Code Vote up 5 votes
public static File getBooheeDir() {
    if (!hasSdcard()) {
        return null;
    }
    File booheeDir = new File(Environment.getExternalStorageDirectory(), BOOHEE_DIR);
    if (booheeDir.exists()) {
        return booheeDir;
    }
    booheeDir.mkdir();
    return booheeDir;
}
 
Example 14
Project: ReactNativeAndroidAudioConverter   File: ReactNativeAndroidAudioConverterModule.java   View Source Code Vote up 5 votes
/**
 * Function to convert a given audio file to MP3 format
 *
 * @param fileName        - input file name
 * @param errorCallback   - function called if the file could not be converted
 * @param successCallback - function called if the conversion was successful
 */
@ReactMethod
public void convertAudioFile(String fileName,
                             final Callback errorCallback,
                             final Callback successCallback) {
    File flacFile = new File(Environment.getExternalStorageDirectory(), fileName);
    Log.d("Output file location", Environment.getExternalStorageDirectory().toString() + fileName);


    IConvertCallback callback = new IConvertCallback() {
        @Override
        public void onSuccess(File convertedFile) {
            // So fast? Love it!
            Log.d("rnaac notice", "convert success");
            successCallback.invoke("convert success");
        }

        @Override
        public void onFailure(Exception error) {
            // Oops! Something went wrong
            errorCallback.invoke("convert failure: " + error.toString());
            Log.d("rnaac notice", error.toString());
            error.printStackTrace();
        }
    };
    AndroidAudioConverter.with(this.getReactApplicationContext())
            // Your current audio file
            .setFile(flacFile)

            // Your desired audio format
            .setFormat(AudioFormat.MP3)

            // An callback to know when conversion is finished
            .setCallback(callback)

            // Start conversion
            .convert();
}
 
Example 15
Project: ScaleSketchPadDemo   File: BitmapUtils.java   View Source Code Vote up 4 votes
public static File getSavePath(Context context){
    if(isExternalStorageWritable()){
        return Environment.getExternalStorageDirectory();
    }
    return context.getCacheDir();
}
 
Example 16
Project: TestChat   File: MapActivity.java   View Source Code Vote up 4 votes
private String getScreenShotLocalPath() {
        return Environment.getExternalStorageDirectory() + File.separator + "TestChat" + File.separator + TimeUtil.getTime(System.currentTimeMillis()) + ".png";
}
 
Example 17
Project: fast_face_android   File: Constants.java   View Source Code Vote up 4 votes
/**
 * getFaceShapeModelPath
 * @return default face shape model path
 */
public static String getFaceShapeModelPath() {
    File sdcard = Environment.getExternalStorageDirectory();
    String targetPath = sdcard.getAbsolutePath() + File.separator + "shape_predictor_68_face_landmarks.dat";
    return targetPath;
}
 
Example 18
Project: ScaleSketchPadDemo   File: BitmapUtils.java   View Source Code Vote up 4 votes
public static File getSavePath(Context context){
    if(isExternalStorageWritable()){
        return Environment.getExternalStorageDirectory();
    }
    return context.getCacheDir();
}
 
Example 19
Project: MetadataEditor   File: FolderPickerConfig.java   View Source Code Vote up 4 votes
private FolderPickerConfig() {
    defaultDirectory = new File(Environment.getExternalStorageDirectory());
}
 
Example 20
Project: ExoPlayer-Offline   File: PlayerActivity.java   View Source Code Vote up 4 votes
private File getUriForManifest(String path) {
    File file1 = Environment.getExternalStorageDirectory();
    File root = new File(file1.getPath() + "/" + path);
    File mpd = new File(root, "Manifest.mpd");
    return mpd;
}