Android 怎么读取指定目录下的音乐文件

Android 怎么读取指定目录下的音乐文件,第1张

ContentResolver mResolver = getContentResolver();

Cursor cursor = mResolverquery(MediaStoreAudioMediaEXTERNAL_CONTENT_URI, null, null, null, MediaStoreAudioMediaDEFAULT_SORT_ORDER);

int i = 0;

int cursorCount = cursorgetCount();

if (cursorCount >0 )

{

cursormoveToFirst();

while (i < cursorCount)

{

//歌曲文件路径 :MediaStoreAudioMediaDATA

url = cursorgetString(cursorgetColumnIndexOrThrow(MediaStoreAudioMediaDATA));

if(urltoLowerCase()indexOf("指定的歌曲路径") > 0)

{

}

i++;

cursormoveToNext();

}

cursorclose();

}

android无法获取res资源文件夹路径,只能通过系统提供的封装函数访问。

资源文件夹有:

/res/drawable

,通过getresources()访问

/res/values

,通过getresources()访问

/res/layout,通过getresources()访问

/res/xml,通过getresources()访问

/res/raw,通过getresources()访问

/assets,通过getassets()访问

1、鼠标右击文件,在d出的菜单选择“File Path”;

2、在d出的小窗口从下往上读就是当前文件的电脑路径,单击最上面的文件就可以打开文件所在位置;

文件在电脑的路径是:D:\MyApplication\appTest\src\main\java\com\example\mytest

直接调用文件管理器选择即可。

1、调用系统提供的选择器,代码如下:

//注意,在Android44系统下建议使用 IntentACTION_OPEN_DOCUMENT方式

if (UtilityisKK()) {

Intent intent = new Intent(IntentACTION_OPEN_DOCUMENT);

intentaddCategory(IntentCATEGORY_OPENABLE);

intentsetType("image

public static String getDataColumn(Context context, Uri uri, String selection,

String[] selectionArgs) {

Cursor cursor = null;

final String column = "_data";

final String[] projection = {

column

};

处理返回结果:

protected void onActivityResult(int requestCode, int resultCode,

Intent intent) {

superonActivityResult(requestCode, resultCode, intent);

if (resultCode == RESULT_OK) {

switch (requestCode) {

case PIC_RESULT://选择图库

case PIC_RESULT_KK:

imageFileUri = intentgetData();//获取选择的URI

break;

2、除此自外,系统还提供一种选择器,这个选择器可以屏蔽掉那个auto backup的目录所以就开始打算用这个选择器来选了

Intent intent=new Intent(IntentACTION_GET_CONTENT);//ACTION_OPEN_DOCUMENT

intentaddCategory(IntentCATEGORY_OPENABLE);

intentsetType("image/jpeg");

if(androidosBuildVERSIONSDK_INT>=androidosBuildVERSION_CODESKITKAT){

startActivityForResult(intent, SELECT_PIC_KITKAT);

}else{

startActivityForResult(intent, SELECT_PIC);

}

为什么要分开不同版本呢其实在43或以下可以直接用ACTION_GET_CONTENT的,在44或以上,官方建议用ACTION_OPEN_DOCUMENT,主要区别是他们返回的Uri43返回的是带文件路径的,而44返回的却是content://comandroidprovidersmediadocuments/document/image:3951这样的,没有路径,只有编号的uri可以通过以下方式,处理URI。

参考:Android 44从图库选择,获取路径并裁剪

public static String getPath(final Context context, final Uri uri) {

final boolean isKitKat = BuildVERSIONSDK_INT >= BuildVERSION_CODESKITKAT;

// DocumentProvider

if (isKitKat && DocumentsContractisDocumentUri(context, uri)) {

// ExternalStorageProvider

if (isExternalStorageDocument(uri)) {

final String docId = DocumentsContractgetDocumentId(uri);

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

final String type = split[0];

if ("primary"equalsIgnoreCase(type)) {

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

}

// TODO handle non-primary volumes

}

// DownloadsProvider

else if (isDownloadsDocument(uri)) {

final String id = DocumentsContractgetDocumentId(uri);

final Uri contentUri = ContentUriswithAppendedId(

Uriparse("content://downloads/public_downloads"), LongvalueOf(id));

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

}

// MediaProvider

else if (isMediaDocument(uri)) {

final String docId = DocumentsContractgetDocumentId(uri);

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

final 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;

}

final String selection = "_id=";

final String[] selectionArgs = new String[] {

split[1]

};

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

}

}

// MediaStore (and general)

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

// Return the remote address

if (isGooglePhotosUri(uri))

return urigetLastPathSegment();

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

}

// File

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

return urigetPath();

}

return null;

}

public static String getDataColumn(Context context, Uri uri, String selection,

String[] selectionArgs) {

Cursor cursor = null;

final String column = "_data";

final String[] projection = {

column

};

try {

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

null);

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

final int index = cursorgetColumnIndexOrThrow(column);

return cursorgetString(index);

}

} finally {

if (cursor != null)

cursorclose();

}

return null;

}

public static boolean isExternalStorageDocument(Uri uri) {

return "comandroidexternalstoragedocuments"equals(urigetAuthority());

}

public static boolean isDownloadsDocument(Uri uri) {

return "comandroidprovidersdownloadsdocuments"equals(urigetAuthority());

}

public static boolean isMediaDocument(Uri uri) {

return "comandroidprovidersmediadocuments"equals(urigetAuthority());

}

public static boolean isGooglePhotosUri(Uri uri) {

return "comgoogleandroidappsphotoscontent"equals(urigetAuthority());

}

3、使用其它开源组件如PhotoView。

EnvironmentgetDataDirectory()getPath() : /data

EnvironmentgetDownloadCacheDirectory()getPath() : /cache

EnvironmentgetExternalStorageDirectory()getPath() : /mnt/sdcard

EnvironmentgetRootDirectory()getPath() : /system

ContextgetCacheDir()getPath() : /data/data/包名/cache

ContextgetExternalCacheDir()getPath() : /mnt/sdcard/Android/data/包名/cache

ContextgetFilesDir()getPath() : /data/data/包名/files

ContextgetObbDir()getPath() : /mnt/sdcard/Android/obb/包名

ContextgetPackageName() : 包名

ContextgetPackageCodePath() : /data/app/应用名

ContextgetPackageResourcePath() : /data/app/应用名

这个是我以前在网上看过的,关于android中具体路径与获取方式之间对应关系的描述。

/data/data/cntonyapp/files/这种路径,可能是在包名为“cntonyapp”的应用下,通过在Context的子类(Activity,Service等等)中调用getFilesDir所得到的。

而/mnt/sdcard/,应该是在40以下的android系统上,通过Environment类的getExternalStorageDirectory方法获取的。

其实getExternalStorageDirectory这个方法,不如说是获取默认存储器的。在不同版本不同设置的android系统上,指向的位置也不太一样。比如在40以上的系统上,它一般指向“storage/sdcard0”(内置SD卡),但也有厂家或个人把它设置为“storage/sdcard1”(外置SD卡)。就像window,系统一般是装在C盘,但装在D盘上其实也可以。

至于区别,我觉得主要是与外置SD卡有关。android44系统对外置SD卡做了一定限制。在它的设置中,手机内存储(内置SD卡)属于共有资源,只要有权限,各种应用都能可以上面自由的读写文件夹和文件。而外置SD卡则管的比较严,它给每个第三方应用都划了块“个人空间”。就是“data/data/[包名]”目录,应用只能在它自己的地盘上活动( *** 作文件)。举例来说,如果我有个包名叫a的应用,它可以在内置SD卡上任意位置创建读写文件,而对于外置sd卡,则只能 *** 作"data/data/a"目录下的文件。这种方式文件结构比较清晰,同时还避免了一些隐患,删除时也方便。

PS:有些44以上的手机,SD卡仍是一团乱。可能有两个原因,一个是系统在SD卡上创建文件,这个是不受限的;还有一个就是上面说的,把默认设为了外存储,纯粹属于把外置SD卡当内置SD使了。

/data/data/comandroidprovidersmedia/databases

internaldb

external-XXXXdb

参考:>

以上就是关于Android 怎么读取指定目录下的音乐文件全部的内容,包括:Android 怎么读取指定目录下的音乐文件、android下怎么获取res资源文件夹的路径、Android studio怎么找到当前文件在电脑路径位置等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存