下面列出了android.os.Environment#DIRECTORY_DOWNLOADS 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void addCommonBookmarks() {
String sd = Environment.getExternalStorageDirectory() + "/";
String[] dirs = new String[] {
sd + Environment.DIRECTORY_DCIM,
sd + Environment.DIRECTORY_DOWNLOADS,
sd + Environment.DIRECTORY_MOVIES,
sd + Environment.DIRECTORY_MUSIC,
sd + Environment.DIRECTORY_PICTURES
};
for (String dir : dirs) {
addBookmark(new File(dir).getName(), dir);
}
}
private String getDirectoryName(String name) {
switch (name) {
case "ALARMS":
return Environment.DIRECTORY_ALARMS;
case "DCIM":
return Environment.DIRECTORY_DCIM;
case "DOCUMENTS":
return Environment.DIRECTORY_DOCUMENTS;
case "DOWNLOADS":
return Environment.DIRECTORY_DOWNLOADS;
case "MOVIES":
return Environment.DIRECTORY_MOVIES;
case "MUSIC":
return Environment.DIRECTORY_MUSIC;
case "NOTIFICATIONS":
return Environment.DIRECTORY_NOTIFICATIONS;
case "PICTURES":
return Environment.DIRECTORY_PICTURES;
case "PODCASTS":
return Environment.DIRECTORY_PODCASTS;
case "RINGTONES":
return Environment.DIRECTORY_RINGTONES;
default:
throw new IllegalArgumentException("Invalid directory representation: " + name);
}
}
@Test
public void testGetters() {
final Download download = new Download(
"https://www.mozilla.org/image.png",
"Focus/1.0",
"Content-Disposition: attachment; filename=\"filename.png\"",
"image/png",
1024,
Environment.DIRECTORY_DOWNLOADS,
fileName);
assertEquals("https://www.mozilla.org/image.png", download.getUrl());
assertEquals("Focus/1.0", download.getUserAgent());
assertEquals("Content-Disposition: attachment; filename=\"filename.png\"", download.getContentDisposition());
assertEquals("image/png", download.getMimeType());
assertEquals(1024, download.getContentLength());
assertEquals(Environment.DIRECTORY_DOWNLOADS, download.getDestinationDirectory());
assertEquals(fileName, download.getFileName());
}
public static String findPackageObbFile(String packageName)
{
final String[] pathsToLook = { getObbLocation(packageName),
// For development purpose - obbs can be in MWM folder, if MWM is installed too
Environment.getExternalStorageDirectory() + File.separator + "MapsWithMe",
// For user-testing, to directly download obb to the device by http link
Environment.getExternalStorageDirectory() + File.separator + Environment.DIRECTORY_DOWNLOADS };
for (final String pathToLook : pathsToLook)
{
final File obbDir = new File(pathToLook);
if (!obbDir.exists())
continue;
final String[] filesInDir = obbDir.list();
for (final String fileName : filesInDir)
if (fileName.endsWith(packageName + ".obb"))
return obbDir.getAbsolutePath() + File.separator + fileName;
}
// obb was not found :(
return null;
}
@Nullable
private String getOutputPathForJob(@NonNull DownloadJob job) {
File outputFolder = new File(mContext.getExternalFilesDir(null), Environment.DIRECTORY_DOWNLOADS);
if (outputFolder.exists() || (!outputFolder.exists() && outputFolder.mkdir())) {
File outputFile = new File(outputFolder, job.getFilename());
return "file://" + outputFile.getAbsolutePath();
}
return null;
}
private static void dirChecker(String dir) {
File f = new File(Environment.DIRECTORY_DOWNLOADS + dir);
if(!f.isDirectory()) {
f.mkdirs();
}
}
@Test
public void listFilesZeroFiles() throws Exception {
File tempFile = new File(Environment.DIRECTORY_DOWNLOADS);
File directory = new File(tempFile, "rajababu");
directory.mkdir();
ArrayList files = new ArrayList();
FileUtils.listFiles(directory, files);
assertEquals(0, files.size());
// clean up
directory.delete();
}
public ArrayList<Uri> getURI() {
final MimeTypeMap mime = MimeTypeMap.getSingleton();
ArrayList<Uri> finalUris = new ArrayList<>();
for (int uriIndex = 0; uriIndex < this.uris.size(); uriIndex++) {
Uri uri = this.uris.get(uriIndex);
if(this.isBase64File(uri)) {
String type = uri.getSchemeSpecificPart().substring(0, uri.getSchemeSpecificPart().indexOf(";"));
String extension = mime.getExtensionFromMimeType(type);
String encodedImg = uri.getSchemeSpecificPart().substring(uri.getSchemeSpecificPart().indexOf(";base64,") + 8);
String fileName = filenames.size() >= uriIndex + 1 ? filenames.get(uriIndex) : (System.currentTimeMillis() + "." + extension);
try {
File dir = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOWNLOADS );
if (!dir.exists() && !dir.mkdirs()) {
throw new IOException("mkdirs failed on " + dir.getAbsolutePath());
}
File file = new File(dir, fileName);
final FileOutputStream fos = new FileOutputStream(file);
fos.write(Base64.decode(encodedImg, Base64.DEFAULT));
fos.flush();
fos.close();
finalUris.add(RNSharePathUtil.compatUriFromFile(reactContext, file));
} catch (IOException e) {
e.printStackTrace();
}
} else if(this.isLocalFile(uri)) {
if (uri.getPath() != null) {
if (filenames.size() >= uriIndex + 1) {
finalUris.add(RNSharePathUtil.compatUriFromFile(reactContext, new File(uri.getPath(), filenames.get(uriIndex))));
} else {
finalUris.add(RNSharePathUtil.compatUriFromFile(reactContext, new File(uri.getPath())));
}
}
}
}
return finalUris;
}
public static String getStorageDirectory(Context mContext) {
File path = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
Log.w("[File Utils] External storage is mounted");
String directory = Environment.DIRECTORY_DOWNLOADS;
path = mContext.getExternalFilesDir(directory);
}
if (path == null) {
Log.w("[File Utils] Couldn't get external storage path, using internal");
path = mContext.getFilesDir();
}
return path.getAbsolutePath();
}
private DownloadManager.Request createRequest(@NonNull String url,
@NonNull String filename,
@Nullable String destinationPath,
@NonNull String mimeType,
boolean inPublicDir,
boolean showCompletedNotification) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription(filename);
request.setMimeType(mimeType);
if (destinationPath == null) {
destinationPath = Environment.DIRECTORY_DOWNLOADS;
}
File destinationFolder = inPublicDir
? Environment.getExternalStoragePublicDirectory(destinationPath)
: new File(context.getFilesDir(), destinationPath);
createFolderIfNeeded(destinationFolder);
removeDuplicateFileIfExist(destinationFolder, filename);
if (inPublicDir) {
request.setDestinationInExternalPublicDir(destinationPath, filename);
} else {
request.setDestinationInExternalFilesDir(context, destinationPath, filename);
}
request.setNotificationVisibility(showCompletedNotification
? DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED
: DownloadManager.Request.VISIBILITY_VISIBLE);
return request;
}
public static File getDownloadDir() throws NoExternalStorageException {
return new File(getSignalStorageDir(), Environment.DIRECTORY_DOWNLOADS);
}
public static File getDownloadDir() throws NoExternalStorageException {
return new File(getExternalStorageDir(), Environment.DIRECTORY_DOWNLOADS);
}
public static File getDownloadDir() throws NoExternalStorageException {
return new File(getStorageDir(), Environment.DIRECTORY_DOWNLOADS);
}
public static File getDownloadsFolder() {
return new File("/sdcard/" + Environment.DIRECTORY_DOWNLOADS);
}