android开发中怎么在资源文件中获取mp3文件

android开发中怎么在资源文件中获取mp3文件,第1张

文件放在res/raw下,程序运行时把它释放到指定目录,代码如下:(供楼主参考)

private final String DATABASE_PATH = androidosEnvironmentgetExternalStorageDirectory()getAbsolutePath() + "/db_exam";

private final String DATABASE_FILENAME = "teldb";

public void extractDBFileFromRes(){

try {

String dbFileName = DATABASE_PATH + "/" + DATABASE_FILENAME;

File dir = new File(DATABASE_PATH);

if (!direxists()){

dirmkdir();

Logi("SQLite", "dir made:" + DATABASE_PATH);

} else {

Logi("SQLite", "dir exist:" + DATABASE_PATH);

}

try {

//如果数据库已经在SD卡的目录下存在,那么不需要重新创建,否则创建文件,并拷贝/res/raw下面的数据库文件

if (!(new File(dbFileName)exists())){

Logi("SQLite", dbFileName + ":file not exist");

//res/raw数据库作为输出流

InputStream inputStream = thisgetResources()openRawResource(Rrawtel);

//测试

int size = inputStreamavailable();

Logi("SQLite", "DATABASE_SIZE:" + 1);

Logi("SQLite", "count:" + 0);

//用于存放数据库信息的数据流

FileOutputStream fileOutputStream = new FileOutputStream(dbFileName);

byte[] buffer = new byte[8192];

int count = 0;

Logi("SQLite", "count:" + count);

//把数据写入SD卡目录下

while ((count = inputStreamread(buffer)) > 0 ) {

fileOutputStreamwrite(buffer, 0, count);

}

fileOutputStreamflush();

fileOutputStreamclose();

inputStreamclose();

}

} catch (FileNotFoundException e) {

Loge("Database", "File not found");

eprintStackTrace();

}

} catch (IOException e) {

Loge("Database", "IO exception");

eprintStackTrace();

}

}

These files will be ones that get deleted first when the device runs low on storage There is no guarantee when these files will be deleted

但是,最好不要依赖系统来管理,应该自己设定一个最大容量,当超出这个值时自己删除。

ContextgetFilesDir(),ContextopenFileOutput(String, int),ContextgetFileStreamPath(String),ContextgetDir(String, int)

/data/data/files

Android支持在SD卡上的应用私有目录,在Froyo版本后,通过getExternalFilesDir()可以获得具体路径。该路径依赖与应用的包名,如果你包为hellofile那么SD开上的应用私有目录为\mnt\sdcard\Android\data\hellofile\files\

在使用SD卡目录时,需注意SD卡是否挂载,可通过EnvironmentgetExternalStorageState()方法进行判断,如果返回值为EnvirnmentMEDIA_MOUNTED表示SD卡处于挂载状态,可以放心使用。

getExternalCacheDir()和getCacheDir()比较

共同点:

files will be deleted when the application is uninstalled

不同点:

1、The platform does not monitor the space available in external storage, and thus will not automatically delete these files Note that you should be managing the maximum space you will use for these anyway, just like with getCacheDir()

2、External files are not always available: they will disappear if the user mounts the external storage on a computer or removes it See the APIs on Environment for information in the storage state

3、There is no security enforced with these files All applications can read and write files placed here

安卓中如何获取保存的uri 并保存到sqlite数据库中

有如下两种方法,仅供参考

方法一:Java代码

public void saveIcon(Bitmap icon) {

if (icon == null) {

return;

}

// 最终图标要保存到浏览器的内部数据库中,系统程序均保存为SQLite格式,Browser也不例外,因为是二进制的所以使用字节数组存储数据库的

// BLOB类型

final ByteArrayOutputStream os = new ByteArrayOutputStream();

// 将Bitmap压缩成PNG编码,质量为100%存储

iconcompress(BitmapCompressFormatPNG, 100, os);

// 构造SQLite的Content对象,这里也可以使用

raw ContentValues values = new ContentValues();

// 写入数据库的

BrowserBookmarkColumnsTOUCH_ICON字段 valuesput(BrowserBookmarkColumnsTOUCH_ICON, ostoByteArray());

DBUtilupdate();

//调用更新或者插入到数据库的方法

}

}

方法二:如果数据表入口时一个content:URIJava代码

import androidproviderMediaStoreImagesMedia;

import androidcontentContentValues;

import javaioOutputStream;

// Save the name and description of an image in a ContentValues map

ContentValues values = new ContentValues(3);

valuesput(MediaDISPLAY_NAME, "road_trip_1");

valuesput(MediaDESCRIPTION, "Day 1, trip to Los Angeles");

valuesput(MediaMIME_TYPE, "image/jpeg");

// Add a new record without the bitmap, but with the values just set

// insert() returns the URI of the new record

Uri uri = getContentResolver()insert(MediaEXTERNAL_CONTENT_URI, values);

// Now get a handle to the file for that record, and save the data into it

// Here, sourceBitmap is a Bitmap object representing the file to save to the database

try {

OutputStream outStream = getContentResolver()openOutputStream(uri);

sourceBitmapcompress(BitmapCompressFormatJPEG, 50, outStream);

outStreamclose();

} catch (Exception e) {

Loge(TAG, "exception while writing image", e);

}

原文请看>

可以按以下步骤进行:

Android应用的数据库一般都是私有的,其他应用无法访问,那么怎么在手机已root的前提下,在自己的应用中读取指定应用中的数据信息呢,现提供一种思路。

以uc浏览器历史浏览记录为例:

一:对手机进行root。

某些厂家的Android设备是支持在系统设置中一键root的,如魅族手机。更通用的情况下一般是用第三方软件进行root,如KingRoot。

二:在手机上安装RootExplorer。

RootExplorer是在Android上使用很方便的文件浏览器,借用它,我们可以找到uc浏览器的历史记录数据库所在为data/data/comUCMobile/databases/history/history。

三:将数据库文件复制到sd卡指定目录。

四:从sd卡数据库文件中读取数据。

五:将数据加载到Recyclerview中展示。

至此,我们已经实现了在自己应用中读取其他应用数据库数据的一个完整过程,诚然这种方式存在许多局限性,但不失为解决方案的一种。

打开AndroidStudio,找到DeviceFileExporler,

2

选择展开的目录data->data->项目包名的目录->database目录->想要导出的db数据库->右键save as

3

选择保存本地目录

4

查看数据库

查看更多

数据库很多,但是安卓直接支持的数据库只有sqlite一种。如果要使用其它的非本地数据库,你得建立连接,采用webservice或>

以上就是关于android开发中怎么在资源文件中获取mp3文件全部的内容,包括:android开发中怎么在资源文件中获取mp3文件、Android 请问如何更改android 数据库的存储路径、android 如何获取保存的图片的地址 并存到数据库中等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址: http://outofmemory.cn/web/9464344.html

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

发表评论

登录后才能评论

评论列表(0条)

保存