‘壹’ android开发,把工程目录里面raw目录下的几张照片存放到SD卡里面去。
java">//name要创建的文件的名字,id是raw那个文件的id,R.raw.xxx就可以了
publicvoidcreateFile(Stringname,intid){
StringfilePath=ConstantPool.EXP_MUSIC+"/"+name;//文件路径
try{
Filedir=newFile(ConstantPool.EXP_MUSIC);//目录路径
if(!dir.exists()){//如果不存在,则创建路径名
System.out.println("要存储的目录不存在");
if(dir.mkdirs()){//创建该路径名,返回true则表示创建成功
System.out.println("已经创建文件存储目录");
}else{
System.out.println("创建目录失败");
}
}
//目录存在,则将apk中raw中的需要的文档复制到该目录下
Filefile=newFile(filePath);
if(!file.exists()){//文件不存在
System.out.println("要打开的文件不存在");
InputStreamins=context.getResources().openRawResource(
id);//通过raw得到数据资源
System.out.println("开始读入");
FileOutputStreamfos=newFileOutputStream(file);
System.out.println("开始写出");
byte[]buffer=newbyte[8192];
intcount=0;//循环写出
while((count=ins.read(buffer))>0){
fos.write(buffer,0,count);
}
System.out.println("已经创建该文件");
fos.close();//关闭流
ins.close();
}
}catch(Exceptione){
e.printStackTrace();
}
}
‘贰’ android 如何批量获取raw文件夹中的文件
HER
‘叁’ Android开发过程中遇到的问题。关于res目录下raw中的txt文件引用问题
InputStream is = getResources().openRawResource(R.id.fileNameID) ;
//R.id.fileNameID为需要访问的文件对应的资源ID.接着我们就可以通过输入流来读取相应文件的内容了。
你这个R.raw.sgyy只是一个生成的id值,不是路径。需要通过android提供的方法来打开具体的资源内容。
‘肆’ android studio videoview播放路径设置
path 获取路径视频文件夹写raw文件夹
/**
* raw文件夹文件处理工具类
*
* */
public class RawFileUtils {
private RawFileUtils( ){
}
/**
* 读取raw文件夹文件
* @param resourceId raw文件夹文件资源ID
* @return 文件内容
*
* */
public static String readFileFromRaw(Context context, int resourceId) {
if( null == context || resourceId < 0 ){
return null;
}
String result = null;
try {
InputStream inputStream = context.getResources().openRawResource( resourceId );
// 获取文件字节数
int length = inputStream.available();
// 创建byte数组
byte[] buffer = new byte[length];
// 文件数据读byte数组
inputStream.read(buffer);
result = EncodingUtils.getString(buffer, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
path=RawFileUtils.readFileFromRaw(mContext, resourceId );
‘伍’ android的项目里怎么规定文件路径的
方法一:把目标文件放入resources文件中,以通过读取R的资源文件来获取,具体方式如下:
1、在res下新建raw文件,将带读取文件添加到raw文件目录下。
2、添加如下代码:
// 如果要使用文件名获取文件数据:首先获取资源id然后再通过id获取输入流
InputStream im = getResources().openRawResource(R.raw.h_data11);
BufferedReader read = new BufferedReader(new InputStreamReader(im));
String line = "";
StringBuilder sb = new StringBuilder();
try {
while((line = read.readLine()) != null) {
sb.append(line).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(read != null) {
try {
read.close();
read = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if(im != null) {
try {
im.close();
im = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
Log.v("", "result = " + sb.toString());
方法二:使用assets 只读文件进行读取。
1、将文件到assets下,可以新建文件夹如:“www”然后将文件放入www文件夹中,读取的path为:"www/filename"
String result = "";
ObjectInputStream ois = null;
AssetManager am = context.getResources().getAssets();
try {
ois = new ObjectInputStream(am.open("www/filename"));
result = (String) ois.readObject();
} catch (StreamCorruptedException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (ois != null) {
ois.close();
ois = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
以对象的方式读取文件中的数据,如果没有新建文件夹,把前面的“www/”去掉就ok啦
以上方式我都还有疑问的地方:
1、raw下新建多级目录是否真的不能够使用。
‘陆’ android工程res目录下raw文件夹中的文件绝对路径是什么
raw是程序包里的文件,安装到程序以后也没有绝对路径,因为这个是在程序内部的
但是你可以通过 InputStream is =getResources().openRawResource(R.id.filename);
来得到这个inputStream
‘柒’ android下怎么获取res资源文件夹的路径
android无法获取res资源文件夹路径,只能通过系统提供的封装函数访问。
资源文件夹有:
/res/drawable
,通过getresources()访问
/res/values
,通过getresources()访问
/res/layout,通过getresources()访问
/res/xml,通过getresources()访问
/res/raw,通过getresources()访问
/assets,通过getassets()访问
‘捌’ 关于Android中videoView.setVideoPath(“PATH”)的问题!!!急!!
path 是获取的路径,如果你把视频文件夹写在raw文件夹下 ,
/**
* raw文件夹下的文件处理工具类
*
* */
public class RawFileUtils {
private RawFileUtils( ){
}
/**
* 读取raw文件夹下的文件
* @param resourceId raw文件夹下的文件资源ID
* @return 文件内容
*
* */
public static String readFileFromRaw(Context context, int resourceId) {
if( null == context || resourceId < 0 ){
return null;
}
String result = null;
try {
InputStream inputStream = context.getResources().openRawResource( resourceId );
// 获取文件的字节数
int length = inputStream.available();
// 创建byte数组
byte[] buffer = new byte[length];
// 将文件中的数据读到byte数组中
inputStream.read(buffer);
result = EncodingUtils.getString(buffer, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
然后 path=RawFileUtils.readFileFromRaw(mContext, resourceId );
‘玖’ android下怎么获取res资源文件夹的路径
android无法获取res资源文件夹路径,只能通过系统提供的封装函数访问。
资源文件夹有:
/res/drawable ,通过getResources()访问
/res/values ,通过getResources()访问
/res/layout,通过getResources()访问
/res/xml,通过getResources()访问
/res/raw,通过getResources()访问
/assets,通过getAssets()访问