如何获取文档文件的内部共享存储和sd卡文件路径?
How to get internal shared storage and sd card file paths of document files?
我正在尝试从内部共享存储和 SD 卡获取视频的文件路径。
我已将视频随机保存在内部共享存储和外部存储的随机文件夹中。
public ArrayList<String> getVideosPath(Activity activity,Context context)
{
listOfAllImages = new ArrayList<String>();
Cursor cursor;
final String[] columns = {MediaStore.Video.Media.DATA, MediaStore.Video.Media._ID};
final String orderBy = MediaStore.Video.Media._ID;
//Stores all the images from the gallery in Cursor
cursor = getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy);
//Total number of images
int count = cursor.getCount();
//Create an array to store path to all the images
String[] arrPath = new String[count];
for (int i = 0; i < count; i++) {
cursor.moveToPosition(i);
int dataColumnIndex = cursor.getColumnIndex(MediaStore.Video.Media.DATA);
//Store the path of the image
arrPath[i] = cursor.getString(dataColumnIndex);
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(arrPath[i],
MediaStore.Images.Thumbnails.MINI_KIND);
// BitmapFactory.Options options = new BitmapFactory.Options();
// options.inSampleSize = 4;
// Bitmap b = BitmapFactory.decodeFile(arrPath[i], options);
bitmap.add(thumb);
// drawimages_onvideo.add(thumb);
Log.i("PATH", arrPath[i]);
listOfAllImages.add(arrPath[i]);
}
虽然是从sd卡和phone存储中获取文件,但是路径不同
例如有 2 个视频文件,一个在 android 存储中,一个在 sd 卡中,路径为
android 存储空间:/storage/emulated/0/Movies/videoplay.mp4
和
sd 卡:/storage/BBF7-A8D2/videos/videoplay.mp4
我正在获取这 2 个文件,但显示给我的路径是:
/storage/emulated/0/Movies/videoplay.mp4
/storage/emulated/0/Movies/videoplay.mp4
怎么了?
这给了我所需的文档文件,从堆栈流链接之一获得了帮助。
public void getDocumentspath(){
listOfAllDocuments = new ArrayList<String>();
Cursor cursor;
String root_sd = Environment.getExternalStorageDirectory().toString();
String pdf = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
String doc = MimeTypeMap.getSingleton().getMimeTypeFromExtension("doc");
String docx = MimeTypeMap.getSingleton().getMimeTypeFromExtension("docx");
String xls = MimeTypeMap.getSingleton().getMimeTypeFromExtension("xls");
String xlsx = MimeTypeMap.getSingleton().getMimeTypeFromExtension("xlsx");
String ppt = MimeTypeMap.getSingleton().getMimeTypeFromExtension("ppt");
String pptx = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pptx");
String txt = MimeTypeMap.getSingleton().getMimeTypeFromExtension("txt");
String rtx = MimeTypeMap.getSingleton().getMimeTypeFromExtension("rtx");
String rtf = MimeTypeMap.getSingleton().getMimeTypeFromExtension("rtf");
String html = MimeTypeMap.getSingleton().getMimeTypeFromExtension("html");
String css = MimeTypeMap.getSingleton().getMimeTypeFromExtension("css");
// String js = MimeTypeMap.getSingleton().getMimeTypeFromExtension("js");
//Table
Uri table = MediaStore.Files.getContentUri("external");
//Column
String[] column = {MediaStore.Files.FileColumns.DATA};
//Where
String where = MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?";
//args
String[] args = new String[]{pdf,doc,docx,xls,xlsx,ppt,pptx,txt,rtx,rtf,html,css};
Cursor fileCursor = getContentResolver().query(table, column, where, args, null);
int count = fileCursor.getCount();
//Create an array to store path to all the documents
String[] arrPath = new String[count];
for (int i = 0; i < count; i++) {
fileCursor.moveToPosition(i);
int dataColumnIndex = fileCursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);
//Store the path of the document
arrPath[i] = fileCursor.getString(dataColumnIndex);
Bitmap b = ((BitmapDrawable) ResourcesCompat.getDrawable(this.getResources(), R.drawable.documentfile, null)).getBitmap();
bitmap.add(b);
Log.i("PATH", arrPath[i]);
listOfAllDocuments.add(arrPath[i]);
}
}
我正在尝试从内部共享存储和 SD 卡获取视频的文件路径。
我已将视频随机保存在内部共享存储和外部存储的随机文件夹中。
public ArrayList<String> getVideosPath(Activity activity,Context context)
{
listOfAllImages = new ArrayList<String>();
Cursor cursor;
final String[] columns = {MediaStore.Video.Media.DATA, MediaStore.Video.Media._ID};
final String orderBy = MediaStore.Video.Media._ID;
//Stores all the images from the gallery in Cursor
cursor = getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy);
//Total number of images
int count = cursor.getCount();
//Create an array to store path to all the images
String[] arrPath = new String[count];
for (int i = 0; i < count; i++) {
cursor.moveToPosition(i);
int dataColumnIndex = cursor.getColumnIndex(MediaStore.Video.Media.DATA);
//Store the path of the image
arrPath[i] = cursor.getString(dataColumnIndex);
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(arrPath[i],
MediaStore.Images.Thumbnails.MINI_KIND);
// BitmapFactory.Options options = new BitmapFactory.Options();
// options.inSampleSize = 4;
// Bitmap b = BitmapFactory.decodeFile(arrPath[i], options);
bitmap.add(thumb);
// drawimages_onvideo.add(thumb);
Log.i("PATH", arrPath[i]);
listOfAllImages.add(arrPath[i]);
}
虽然是从sd卡和phone存储中获取文件,但是路径不同
例如有 2 个视频文件,一个在 android 存储中,一个在 sd 卡中,路径为
android 存储空间:/storage/emulated/0/Movies/videoplay.mp4
和
sd 卡:/storage/BBF7-A8D2/videos/videoplay.mp4
我正在获取这 2 个文件,但显示给我的路径是:
/storage/emulated/0/Movies/videoplay.mp4
/storage/emulated/0/Movies/videoplay.mp4
怎么了?
这给了我所需的文档文件,从堆栈流链接之一获得了帮助。
public void getDocumentspath(){
listOfAllDocuments = new ArrayList<String>();
Cursor cursor;
String root_sd = Environment.getExternalStorageDirectory().toString();
String pdf = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
String doc = MimeTypeMap.getSingleton().getMimeTypeFromExtension("doc");
String docx = MimeTypeMap.getSingleton().getMimeTypeFromExtension("docx");
String xls = MimeTypeMap.getSingleton().getMimeTypeFromExtension("xls");
String xlsx = MimeTypeMap.getSingleton().getMimeTypeFromExtension("xlsx");
String ppt = MimeTypeMap.getSingleton().getMimeTypeFromExtension("ppt");
String pptx = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pptx");
String txt = MimeTypeMap.getSingleton().getMimeTypeFromExtension("txt");
String rtx = MimeTypeMap.getSingleton().getMimeTypeFromExtension("rtx");
String rtf = MimeTypeMap.getSingleton().getMimeTypeFromExtension("rtf");
String html = MimeTypeMap.getSingleton().getMimeTypeFromExtension("html");
String css = MimeTypeMap.getSingleton().getMimeTypeFromExtension("css");
// String js = MimeTypeMap.getSingleton().getMimeTypeFromExtension("js");
//Table
Uri table = MediaStore.Files.getContentUri("external");
//Column
String[] column = {MediaStore.Files.FileColumns.DATA};
//Where
String where = MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?";
//args
String[] args = new String[]{pdf,doc,docx,xls,xlsx,ppt,pptx,txt,rtx,rtf,html,css};
Cursor fileCursor = getContentResolver().query(table, column, where, args, null);
int count = fileCursor.getCount();
//Create an array to store path to all the documents
String[] arrPath = new String[count];
for (int i = 0; i < count; i++) {
fileCursor.moveToPosition(i);
int dataColumnIndex = fileCursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);
//Store the path of the document
arrPath[i] = fileCursor.getString(dataColumnIndex);
Bitmap b = ((BitmapDrawable) ResourcesCompat.getDrawable(this.getResources(), R.drawable.documentfile, null)).getBitmap();
bitmap.add(b);
Log.i("PATH", arrPath[i]);
listOfAllDocuments.add(arrPath[i]);
}
}