I have used this for scan all video file on android
public class FileBrowser extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.file_browser);
TextView lists = findViewById(R.id.file_managerTextView);
List<File> files = getAllMediaFilesOnDevice(FileBrowser.this);
for(int i=0;i<files.size();i++){
lists.append(files.get(i).getAbsolutePath() + "\n");
}
}
public static List<File> getAllMediaFilesOnDevice(Context context) {
List<File> files = new ArrayList<>();
try {
Cursor cursor = context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null);
cursor.moveToFirst();
files.clear();
while (!cursor.isAfterLast()){
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
int lastPoint = path.lastIndexOf(".");
path = path.substring(0, lastPoint) + path.substring(lastPoint).toLowerCase();
files.add(new File(path));
cursor.moveToNext();
}
} catch (Exception e) {
e.printStackTrace();
}
return files;
}
}
it's work but problem is given output is from path /storage/emulated/0/
and /sdcard
so this function return duplicate files because files in /storage/emulated/0/
and /sdcard
are same So, my question is how to prevent any of these two path to be scanned by mediastore
A simple solution would be to insert an if condition:
String filepath = files.get(i).getAbsolutePath();
if(!filepath.startsWith("/sdcard"))
{
lists.append(filepath + "\n");
}
You can replace /sdcard
with /storage/emulated/0
depending on which path you would like to use. A word of warning - some phones have different internal storage paths, and using 1 of these 2 might not work properly. For that, you would need to check first, whether the File
object exists & then proceed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.