如何获得assets目录下文件的uri

如何获得assets目录下文件的uri,第1张

你获取的uri没有问题。但assets目录下的不能使用这种方法读取。你得把它先复制到sdcard中,然后从sdcard中传递这个至于原因嘛:assets目录里面的资源文件还在应用程序的apk文件中,一个压缩文件中。所以不能这样读

/

根据Uri获取绝对路径,解决Android44以上版本Uri转换

@param activity

@param imageUri

@author yaoxing

@date 2014-10-12

/

@TargetApi(19)

public static String getImageAbsolutePath(Activity context, Uri imageUri) {

if (context == null || imageUri == null)

return null;

if (androidosBuildVERSIONSDK_INT >= androidosBuildVERSION_CODESKITKAT && DocumentsContractisDocumentUri(context, imageUri)) {

if (isExternalStorageDocument(imageUri)) {

String docId = DocumentsContractgetDocumentId(imageUri);

String[] split = docIdsplit(":");

String type = split[0];

if ("primary"equalsIgnoreCase(type)) {

return EnvironmentgetExternalStorageDirectory() + "/" + split[1];

}

} else if (isDownloadsDocument(imageUri)) {

String id = DocumentsContractgetDocumentId(imageUri);

Uri contentUri = ContentUriswithAppendedId(Uriparse("content://downloads/public_downloads"), LongvalueOf(id));

return getDataColumn(context, contentUri, null, null);

} else if (isMediaDocument(imageUri)) {

String docId = DocumentsContractgetDocumentId(imageUri);

String[] split = docIdsplit(":");

String type = split[0];

Uri contentUri = null;

if ("image"equals(type)) {

contentUri = MediaStoreImagesMediaEXTERNAL_CONTENT_URI;

} else if ("video"equals(type)) {

contentUri = MediaStoreVideoMediaEXTERNAL_CONTENT_URI;

} else if ("audio"equals(type)) {

contentUri = MediaStoreAudioMediaEXTERNAL_CONTENT_URI;

}

String selection = MediaStoreImagesMedia_ID + "=";

String[] selectionArgs = new String[] { split[1] };

return getDataColumn(context, contentUri, selection, selectionArgs);

}

} // MediaStore (and general)

else if ("content"equalsIgnoreCase(imageUrigetScheme())) {

// Return the remote address

if (isGooglePhotosUri(imageUri))

return imageUrigetLastPathSegment();

return getDataColumn(context, imageUri, null, null);

}

// File

else if ("file"equalsIgnoreCase(imageUrigetScheme())) {

return imageUrigetPath();

}

return null;

}

public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

Cursor cursor = null;

String column = MediaStoreImagesMediaDATA;

String[] projection = { column };

try {

cursor = contextgetContentResolver()query(uri, projection, selection, selectionArgs, null);

if (cursor != null && cursormoveToFirst()) {

int index = cursorgetColumnIndexOrThrow(column);

return cursorgetString(index);

}

} finally {

if (cursor != null)

cursorclose();

}

return null;

}

/

@param uri The Uri to check

@return Whether the Uri authority is ExternalStorageProvider

/

public static boolean isExternalStorageDocument(Uri uri) {

return "comandroidexternalstoragedocuments"equals(urigetAuthority());

}

/

@param uri The Uri to check

@return Whether the Uri authority is DownloadsProvider

/

public static boolean isDownloadsDocument(Uri uri) {

return "comandroidprovidersdownloadsdocuments"equals(urigetAuthority());

}

/

@param uri The Uri to check

@return Whether the Uri authority is MediaProvider

/

public static boolean isMediaDocument(Uri uri) {

return "comandroidprovidersmediadocuments"equals(urigetAuthority());

}

/

@param uri The Uri to check

@return Whether the Uri authority is Google Photos

/

public static boolean isGooglePhotosUri(Uri uri) {

return "comgoogleandroidappsphotoscontent"equals(urigetAuthority());

}

`URI'的意思:

1上呼吸道感染(Upper Respiratory Infection)

2统一资源标识符(Uniform Resource Identifier)

3 乌里州

中文:乌里州;英语:Uri;日语:ウーリ州;法语:Canton d'Uri;韩语:우리 주。

4通用资源识别号(Universal Resource Identifier)

例句

1After you get the URI, you can use it to get the resource template for eachrecord type

在您得到 URI 之后,您可以使用它来为每一个记录类型得到资源模板。

2More importantly, it does not have to assume anything about the structure orsyntax of the URI it used to fetch that representation

更重要的是,它不必对它用来获取那个表示的URI进行任何结构或者语法方面的假设。

我们可以根据微信的开发者文档 >

最近做项目要通过的绝对路径找到的URI,然后删除,小小总结一下获取URI的方法,亲自试验在

android 413的系统上都是可用的。

1将所有的路径取出,遍历比较找到需要的路径,取出URI,效率较低

其中 MediaStoreMediaColumnsDATA 字段存的就是的绝对路径,

最后mImageUri得到的就是的URI

1 Uri mUri = Uriparse("content://media/external/images/media");

2 Uri mImageUri = null;

3 Cursor cursor = managedQuery(

4 MediaStoreImagesMediaEXTERNAL_CONTENT_URI, null, null, null,

5 MediaStoreImagesMediaDEFAULT_SORT_ORDER);

6 cursormoveToFirst();

7

8 while (!cursorisAfterLast()) {

9 String data = cursorgetString(cursor

10 getColumnIndex(MediaStoreMediaColumnsDATA));

11 if (picPathequals(data)) {

12 int ringtoneID = cursorgetInt(cursor

13 getColumnIndex(MediaStoreMediaColumns_ID));

14 mImageUri = UriwithAppendedPath(mUri, "" + ringtoneID);

15 break;

16 }

17 cursormoveToNext();

18 }

2直接从媒体数据库根据字段取出相应的记录,效率较高

1 //TYLT: add by duanyf 20121027 start

2 String type = UtilsensureNotNull(intentgetType());

3 Logd(TAG, "uri is " + uri);

4 if (urigetScheme()equals("file") && (typecontains("image/")))

{

5 String path = urigetEncodedPath();

6 Logd(TAG, "path1 is " + path);

7 if (path != null) {

8 path = Uridecode(path);

9 Logd(TAG, "path2 is " + path);

10 ContentResolver cr = thisgetContentResolver();

11 StringBuffer buff = new StringBuffer();

12 buffappend("(")

13 append(ImagesImageColumnsDATA)

14 append("=")

15 append("'" + path + "'")

16 append(")");

17 Cursor cur = crquery(

18 ImagesMediaEXTERNAL_CONTENT_URI,

19 new String[] { ImagesImageColumns_ID },

20 bufftoString(), null, null);

21 int index = 0;

22 for (curmoveToFirst(); !curisAfterLast(); cur

23 moveToNext()) {

24 index = curgetColumnIndex(ImagesImageColumns_ID);

25 // set _id value

26 index = curgetInt(index);

27 }

28 if (index == 0) {

29 //do nothing

30 } else {

31 Uri uri_temp = Uri

32 parse("content://media/external/images/media/"

33 + index);

34 Logd(TAG, "uri_temp is " + uri_temp);

35 if (uri_temp != null) {

36 uri = uri_temp;

37 }

38 }

39 }

40 }

41 //TYLT: add by duanyf 20121027 end

3直接根据路径通过 ContentProvider 的 delete() 方法删除,两行代码搞定,效率最高

1 String params[] = new String[]{filepath};

2

ctxgetContentResolver()delete(MediaStoreImagesMediaEXTERNAL_CONTENT_URI,

MediaStoreImagesMediaDATA + " LIKE ", params);

以上就是关于如何获得assets目录下文件的uri全部的内容,包括:如何获得assets目录下文件的uri、android4.4版本uri转换怎么调用、uri是什么意思等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9598171.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存