導航:首頁 > 操作系統 > android讀取raw文件的路徑

android讀取raw文件的路徑

發布時間:2022-10-03 12:57:20

『壹』 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()訪問

閱讀全文

與android讀取raw文件的路徑相關的資料

熱點內容
簡訊刪除助手文件夾 瀏覽:686
java辦公自動化 瀏覽:340
php中超鏈接 瀏覽:253
linux默認路由設置 瀏覽:36
linux如何掛載iso 瀏覽:432
vs程序換文件夾後不能編譯 瀏覽:557
安卓源碼編譯輸入腳本沒反應 瀏覽:47
phpmysql自增 瀏覽:167
把ppt保存為pdf 瀏覽:533
汽車密封件加密配件 瀏覽:887
黑馬程序員15天基礎班 瀏覽:560
java調整格式 瀏覽:521
香港雲伺服器租用價 瀏覽:78
linuxsublime3 瀏覽:560
imac混合硬碟命令 瀏覽:277
沈陽用什麼app租房車 瀏覽:857
00後高中生都用什麼app 瀏覽:238
戴爾塔式伺服器怎麼打開獨立顯卡 瀏覽:807
醫療程序員招聘 瀏覽:598
住宿app可砍價是什麼意思 瀏覽:133