-
-
Save sanoj26692/5e5e9baedf0d3821213042581d248fc4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Utils; | |
import android.util.Log; | |
import java.io.File; | |
import java.util.Arrays; | |
import Model.AllVideoModel; | |
import static android.content.ContentValues.TAG; | |
public class ScannedFiles { | |
/*public static void load_Directory_Files(File directory) { | |
File[] fileList = directory.listFiles(); | |
if(fileList != null && fileList.length > 0){ | |
for (int i=0; i<fileList.length; i++){ | |
if(fileList[i].isDirectory()){ | |
load_Directory_Files(fileList[i]); | |
} else { | |
String name = fileList[i].getName().toLowerCase(); | |
for (String extension: Constant.videoExtensions){ | |
//check the type of file | |
if(name.endsWith(extension)){ | |
//Constant.allMediaList.add(fileList[i]); | |
String FileName = fileList[i].getName(); | |
String size = Function.humanReadableByteCountSI(fileList[i].length()); | |
String duration = Function.getDurationSecond(fileList[i].getAbsolutePath()); | |
String folderName = fileList[i].getParentFile().getName(); | |
String path = fileList[i].getAbsolutePath(); | |
Log.d(TAG, "load_Directory_Files: "+ duration + "" +size + "" + FileName); | |
Constant.allMediaListWithModel.add(new AllVideoModel(FileName, path, folderName, size, duration)); | |
//when we found file | |
break; | |
} | |
} | |
} | |
} | |
} | |
}*/ | |
public static void load_Directory_Files(File directory) { | |
File[] files = directory.listFiles(); | |
if (files == null || files.length == 0) { | |
return; | |
} | |
for (File file : files) { | |
if (file.isDirectory()) { | |
load_Directory_Files(file); | |
} else { | |
processFile(file); | |
} | |
} | |
} | |
public static void processFile(File file) { | |
String fileName = file.getName(); | |
String extension = fileName.substring(fileName.lastIndexOf(".")).toLowerCase(); | |
if (!Arrays.asList(Constant.videoExtensions).contains(extension)) { | |
return; | |
} | |
Constant.allMediaList.add(file); | |
String size = Function.humanReadableByteCountSI(file.length()); | |
String duration = Function.getDurationSecond(file.getAbsolutePath()); | |
String folderName = file.getParentFile().getName(); | |
String path = file.getAbsolutePath(); | |
Log.d(TAG, "load_Directory_Files: "+ duration + "" + size + "" + fileName); | |
Constant.allMediaListWithModel.add(new AllVideoModel(fileName, path, folderName, size, duration)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Activity; | |
import android.Manifest; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.content.pm.PackageManager; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.provider.Settings; | |
import android.util.Log; | |
import android.widget.Toast; | |
import Utils.ScannedFiles; | |
import Utils.StorageUtil; | |
import androidx.annotation.NonNull; | |
import androidx.appcompat.app.AlertDialog; | |
import androidx.appcompat.app.AppCompatActivity; | |
import java.io.File; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
/*public class SplashScreen extends AppCompatActivity { | |
private File storage; | |
private String[] storagePaths; | |
@Override | |
public void onCreate(Bundle saveInstanceState) { | |
super.onCreate(saveInstanceState); | |
//load data here | |
storagePaths = StorageUtil.getStorageDirectories(this); | |
long startTime = System.currentTimeMillis(); | |
for (String path : storagePaths) { | |
storage = new File(path); | |
ScannedFiles.load_Directory_Files(storage); | |
} | |
long stopTime = System.currentTimeMillis(); | |
long elapsedTime = stopTime - startTime; | |
Log.d("TAG", "onCreate time Ellapesed: " + elapsedTime); | |
Intent intent = new Intent(SplashScreen.this, MainActivity.class); | |
startActivity(intent); | |
} | |
}*/ | |
public class SplashScreen extends AppCompatActivity { | |
final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124; | |
private File storage; | |
private String[] storagePaths; | |
@Override | |
public void onCreate(Bundle saveInstanceState) { | |
super.onCreate(saveInstanceState); | |
//load data here | |
final Handler handler = new Handler(); | |
handler.postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
if (Build.VERSION.SDK_INT >= 23) { | |
// Marshmallow+ | |
permissioncheck(); | |
} else { | |
// Pre-Marshmallow | |
LaunchApp(); | |
} | |
} | |
}, 3000); | |
} | |
//-************************ permission check *********************************************************************** | |
private void permissioncheck() { | |
List<String> permissionsNeeded = new ArrayList<>(); | |
final List<String> permissionsList = new ArrayList<String>(); | |
if (addPermission(permissionsList, Manifest.permission.READ_EXTERNAL_STORAGE)) | |
permissionsNeeded.add("READ"); | |
if (addPermission(permissionsList, Manifest.permission.WRITE_EXTERNAL_STORAGE)) | |
permissionsNeeded.add("WRITE"); | |
if (permissionsList.size() > 0) { | |
if (permissionsNeeded.size() > 0) { | |
// Need Rationale | |
String message = "You need to grant access to " + permissionsNeeded.get(0); | |
for (int i = 1; i < permissionsNeeded.size(); i++) | |
message = message + ", " + permissionsNeeded.get(i); | |
showMessageOKCancel(message, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
if (Build.VERSION.SDK_INT >= 23) { | |
// Marshmallow+ | |
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS); | |
} else { | |
// Pre-Marshmallow | |
} | |
} | |
}); | |
return; | |
} | |
if (Build.VERSION.SDK_INT >= 23) { | |
// Marshmallow+ | |
requestPermissions(permissionsList.toArray(new String[permissionsList.size()]), REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS); | |
} else { | |
// Pre-Marshmallow | |
} | |
return; | |
} else { | |
// Toast.makeText(this,"Permission",Toast.LENGTH_LONG).show(); | |
LaunchApp(); | |
} | |
//insertDummyContact(); | |
} | |
private boolean addPermission(List<String> permissionsList, String permission) { | |
boolean cond; | |
if (Build.VERSION.SDK_INT >= 23) { | |
// Marshmallow+ | |
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { | |
permissionsList.add(permission); | |
// Check for Rationale Option | |
if (!shouldShowRequestPermissionRationale(permission)) | |
// return false; | |
cond = false; | |
} | |
// return true; | |
cond = true; | |
} else { | |
// Pre-Marshmallow | |
cond = true; | |
} | |
return cond; | |
} | |
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) { | |
new AlertDialog.Builder(SplashScreen.this) | |
.setMessage(message) | |
.setPositiveButton("OK", okListener) | |
.setNegativeButton("Cancel", null) | |
.create() | |
.show(); | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
//Checking the request code of our request | |
if (requestCode == 23) { | |
//If permission is granted | |
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
//Displaying a toast | |
Toast.makeText(this, "Permission granted", Toast.LENGTH_LONG).show(); | |
} else { | |
//Displaying another toast if permission is not granted | |
Toast.makeText(this, "Permission Needed To Run The App", Toast.LENGTH_LONG).show(); | |
} | |
} | |
if (requestCode == REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS) { | |
Map<String, Integer> perms = new HashMap<String, Integer>(); | |
// Initial | |
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED); | |
perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED); | |
perms.put(Manifest.permission.CAMERA, PackageManager.PERMISSION_GRANTED); | |
//Toast.makeText(SplashScreen.this, " Permissions are jddddd", Toast.LENGTH_SHORT).show(); | |
// Fill with results | |
for (int i = 0; i < permissions.length; i++) | |
perms.put(permissions[i], grantResults[i]); | |
// Check for ACCESS_FINE_LOCATION | |
if (perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED | |
&& perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED && | |
perms.get(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { | |
// All Permissions Granted | |
// insertDummyContact(); | |
//Toast.makeText(SplashScreen.this, " Permissions are l", Toast.LENGTH_SHORT).show(); | |
LaunchApp(); | |
} else { | |
// Permission Denied | |
Toast.makeText(SplashScreen.this, "Some Permission is Denied", Toast.LENGTH_SHORT).show(); | |
final Handler handler = new Handler(); | |
handler.postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
//Do something after 100 | |
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); | |
Uri uri = Uri.fromParts("package", getPackageName(), null); | |
intent.setData(uri); | |
startActivityForResult(intent, REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS); | |
finish(); | |
} | |
}, 3000); | |
} | |
} | |
} | |
public void LaunchApp() { | |
Thread background = new Thread() { | |
public void run() { | |
try { | |
// Thread will sleep for 10 seconds | |
storagePaths = StorageUtil.getStorageDirectories(SplashScreen.this); | |
long startTime = System.currentTimeMillis(); | |
for (String path : storagePaths) { | |
storage = new File(path); | |
ScannedFiles.load_Directory_Files(storage); | |
} | |
long stopTime = System.currentTimeMillis(); | |
long elapsedTime = stopTime - startTime; | |
Log.d("TAG", "onCreate time Ellapesed: " + elapsedTime); | |
Intent intent = new Intent(SplashScreen.this, MainActivity.class); | |
startActivity(intent); | |
} catch (Exception e) { } | |
} | |
}; | |
// start thread | |
background.start(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Utils; | |
import android.annotation.SuppressLint; | |
import android.content.Context; | |
import android.os.Build; | |
import android.os.Environment; | |
import android.text.TextUtils; | |
import java.io.File; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Set; | |
/** | |
* Helper class for getting all storages directories in an Android device | |
* <a href="https://stackoverflow.com/a/40582634/3940133">Solution of this problem</a> | |
* Consider to use | |
* <a href="https://developer.android.com/guide/topics/providers/document-provider">StorageAccessFramework(SAF)</> | |
* if your min SDK version is 19 and your requirement is just for browse and open documents, images, and other files | |
* | |
* @author Dmitriy Lozenko, HendraWD | |
*/ | |
public class StorageUtil { | |
// Primary physical SD-CARD (not emulated) | |
private static final String EXTERNAL_STORAGE = System.getenv("EXTERNAL_STORAGE"); | |
// All Secondary SD-CARDs (all exclude primary) separated by File.pathSeparator, i.e: ":", ";" | |
private static final String SECONDARY_STORAGES = System.getenv("SECONDARY_STORAGE"); | |
// Primary emulated SD-CARD | |
private static final String EMULATED_STORAGE_TARGET = System.getenv("EMULATED_STORAGE_TARGET"); | |
// PhysicalPaths based on phone model | |
@SuppressLint("SdCardPath") | |
@SuppressWarnings("SpellCheckingInspection") | |
private static final String[] KNOWN_PHYSICAL_PATHS = new String[]{ | |
"/storage/sdcard0", | |
"/storage/sdcard1", //Motorola Xoom | |
"/storage/extsdcard", //Samsung SGS3 | |
"/storage/sdcard0/external_sdcard", //User request | |
"/mnt/extsdcard", | |
"/mnt/sdcard/external_sd", //Samsung galaxy family | |
"/mnt/sdcard/ext_sd", | |
"/mnt/external_sd", | |
"/mnt/media_rw/sdcard1", //4.4.2 on CyanogenMod S3 | |
"/removable/microsd", //Asus transformer prime | |
"/mnt/emmc", | |
"/storage/external_SD", //LG | |
"/storage/ext_sd", //HTC One Max | |
"/storage/removable/sdcard1", //Sony Xperia Z1 | |
"/data/sdext", | |
"/data/sdext2", | |
"/data/sdext3", | |
"/data/sdext4", | |
"/sdcard1", //Sony Xperia Z | |
"/sdcard2", //HTC One M8s | |
"/storage/microsd" //ASUS ZenFone 2 | |
}; | |
/** | |
* Returns all available storages in the system (include emulated) | |
* <p/> | |
* Warning: Hack! Based on Android source code of version 4.3 (API 18) | |
* Because there is no standard way to get it. | |
* | |
* @return paths to all available storages in the system (include emulated) | |
*/ | |
public static String[] getStorageDirectories(Context context) { | |
// Final set of paths | |
final Set<String> availableDirectoriesSet = new HashSet<>(); | |
if (!TextUtils.isEmpty(EMULATED_STORAGE_TARGET)) { | |
// Device has an emulated storage | |
availableDirectoriesSet.add(getEmulatedStorageTarget()); | |
} else { | |
// Device doesn't have an emulated storage | |
availableDirectoriesSet.addAll(getExternalStorage(context)); | |
} | |
// Add all secondary storages | |
Collections.addAll(availableDirectoriesSet, getAllSecondaryStorages()); | |
String[] storagesArray = new String[availableDirectoriesSet.size()]; | |
return availableDirectoriesSet.toArray(storagesArray); | |
} | |
private static Set<String> getExternalStorage(Context context) { | |
final Set<String> availableDirectoriesSet = new HashSet<>(); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
// Solution of empty raw emulated storage for android version >= marshmallow | |
// because the EXTERNAL_STORAGE become something like: "/Storage/A5F9-15F4", | |
// so we can't access it directly | |
File[] files = getExternalFilesDirs(context); | |
for (File file : files) { | |
if (file != null) { | |
String applicationSpecificAbsolutePath = file.getAbsolutePath(); | |
String rootPath = applicationSpecificAbsolutePath.substring( | |
0, | |
applicationSpecificAbsolutePath.indexOf("Android/data") | |
); | |
availableDirectoriesSet.add(rootPath); | |
} | |
} | |
} else { | |
if (TextUtils.isEmpty(EXTERNAL_STORAGE)) { | |
availableDirectoriesSet.addAll(getAvailablePhysicalPaths()); | |
} else { | |
// Device has physical external storage; use plain paths. | |
availableDirectoriesSet.add(EXTERNAL_STORAGE); | |
} | |
} | |
return availableDirectoriesSet; | |
} | |
private static String getEmulatedStorageTarget() { | |
String rawStorageId = ""; | |
// External storage paths should have storageId in the last segment | |
// i.e: "/storage/emulated/storageId" where storageId is 0, 1, 2, ... | |
final String path = Environment.getExternalStorageDirectory().getAbsolutePath(); | |
final String[] folders = path.split(File.separator); | |
final String lastSegment = folders[folders.length - 1]; | |
if (!TextUtils.isEmpty(lastSegment) && TextUtils.isDigitsOnly(lastSegment)) { | |
rawStorageId = lastSegment; | |
} | |
if (TextUtils.isEmpty(rawStorageId)) { | |
return EMULATED_STORAGE_TARGET; | |
} else { | |
return EMULATED_STORAGE_TARGET + File.separator + rawStorageId; | |
} | |
} | |
private static String[] getAllSecondaryStorages() { | |
if (!TextUtils.isEmpty(SECONDARY_STORAGES)) { | |
// All Secondary SD-CARDs split into array | |
assert SECONDARY_STORAGES != null; | |
return SECONDARY_STORAGES.split(File.pathSeparator); | |
} | |
return new String[0]; | |
} | |
/** | |
* Filter available physical paths from known physical paths | |
* | |
* @return List of available physical paths from current device | |
*/ | |
private static List<String> getAvailablePhysicalPaths() { | |
List<String> availablePhysicalPaths = new ArrayList<>(); | |
for (String physicalPath : KNOWN_PHYSICAL_PATHS) { | |
File file = new File(physicalPath); | |
if (file.exists()) { | |
availablePhysicalPaths.add(physicalPath); | |
} | |
} | |
return availablePhysicalPaths; | |
} | |
/** | |
* Returns absolute paths to application-specific directories on all | |
* external storage devices where the application can place persistent files | |
* it owns. These files are internal to the application, and not typically | |
* visible to the user as media. | |
* <p> | |
* This is like {@link Context#getFilesDir()} in that these files will be | |
* deleted when the application is uninstalled, however there are some | |
* important differences: | |
* <ul> | |
* <li>External files are not always available: they will disappear if the | |
* user mounts the external storage on a computer or removes it. | |
* <li>There is no security enforced with these files. | |
* </ul> | |
* <p> | |
* External storage devices returned here are considered a permanent part of | |
* the device, including both emulated external storage and physical media | |
* slots, such as SD cards in a battery compartment. The returned paths do | |
* not include transient devices, such as USB flash drives. | |
* <p> | |
* An application may store data on any or all of the returned devices. For | |
* example, an app may choose to store large files on the device with the | |
* most available space, as measured by {@link android.os.StatFs}. | |
* <p> | |
* Starting in {@link Build.VERSION_CODES#KITKAT}, no permissions | |
* are required to write to the returned paths; they're always accessible to | |
* the calling app. Before then, | |
* {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} is required to | |
* write. Write access outside of these paths on secondary external storage | |
* devices is not available. To request external storage access in a | |
* backwards compatible way, consider using {@code android:maxSdkVersion} | |
* like this: | |
* | |
* <pre class="prettyprint"><uses-permission | |
* android:name="android.permission.WRITE_EXTERNAL_STORAGE" | |
* android:maxSdkVersion="18" /></pre> | |
* <p> | |
* The first path returned is the same as | |
* {@link Context#getExternalFilesDir(String)}. Returned paths may be | |
* {@code null} if a storage device is unavailable. | |
* | |
* @see Context#getExternalFilesDir(String) | |
*/ | |
private static File[] getExternalFilesDirs(Context context) { | |
return context.getExternalFilesDirs(null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment