[英]How to access video files from sd card folder in android app
我試圖在sdcard中創建的文件夾中顯示listview中的視頻文件。 我正在使用內容提供商,例如:
videocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
但它會讀取存儲在SD卡中的所有視頻文件。 我想只訪問存儲在sdcard文件夾中的文件。 我也用過:
Uri a = Uri.parse(Environment.getExternalStorageDirectory()+"/myfolder");
videocursor = managedQuery(a,proj, null, null, null);
但它給出了錯誤。 有沒有辦法在managedQuery()中包含文件夾路徑或以任何其他方式在listview中顯示文件夾中的視頻文件?
試試這個,
String[] fileList;
File videoFiles = new File(Environment.getExternalStorageDirectory()+"/myfolder");
if(videoFiles.isDirectory())
{
fileList=videoFiles.list();
}
for(int i=0;i<fileList.length;i++)
{
Log.e("Video:"+i+" File name",fileList[i]);
}
我們可以使用如下的managedquerry從SD卡上的指定文件夾中獲取視頻....
videoCursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
videoProjection, MediaStore.Images.Media.DATA + " like ? ",
new String[]{"%M-Videos%"}, null);
嘗試從資源獲取文件,然后放入Uri.fromFile():
private boolean saveAs(int resource) { // save your file in sd card
File root = Environment.getExternalStorageDirectory();
String fileName = "your_file.png";
InputStream input = getBaseContext().getResources().openRawResource(resource);
String path = root.getAbsolutePath();
FileOutputStream save;
byte[] buffer = null;
int size = 0;
try {
size = input.available();
buffer = new byte[size];
input.read(buffer);
input.close();
} catch (IOException e) {
return false;
}
boolean exists = (new File(path)).exists();
if (!exists) {
new File(path).mkdirs();
}
try {
File deleteFile = new File(path + "/" + fileName);
if (deleteFile.exists()) {
deleteFile.delete();
}
save = new FileOutputStream(path + "/" + fileName);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {
return false;
}
return true;
}
}
Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"your_file.png")) // get it from Uri
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.