android下根據路徑不同拷貝檔案至指定路徑
包含content或者file的檔案路徑,eg:content://media/external/audio/media/43870 或者 file://media/external/audio/media/43870,轉換為實際路徑,可以使用
public String getRealPath(Uri fileUrl) {
String fileName = null;
if (fileUrl != null) {
if (fileUrl.getScheme().toString().compareTo("content") == 0) // content://開頭的uri
{
Cursor cursor = this.getContentResolver().query(fileUrl, null,
null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
fileName = cursor.getString(column_index); // 取出檔案路徑
// if (!fileName.startsWith("/mnt")) {// 檢查是否有"/mnt"字首
// fileName = "/mnt" + fileName;
// }
cursor.close();
}
} else if (fileUrl.getScheme().compareTo("file") == 0) { // file:///開頭的uri
fileName = fileUrl.toString();
fileName = fileUrl.toString().replace("file://", "");// 替換file://
// if (!fileName.startsWith("/mnt")) {// 加上"/mnt"頭
// fileName += "/mnt";
// }
}
}
return fileName;
}
註釋的部分為增加/mnt,需要此字首可以增加。
根據uri拷貝檔案至指定路徑下,eg:eg:content://media/external/audio/media/43870,可以用如下方法:
public void copyFile(String audioDst, Uri uri) {
try {
AssetFileDescriptor audioAsset = getContentResolver()
.openAssetFileDescriptor(uri, "r");
InputStream in = audioAsset.createInputStream();
OutputStream out = new FileOutputStream(audioDst);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
根據檔案的絕對路徑拷貝至指定路徑,可以使用如下方法:
public void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //檔案存在時
InputStream inStream = new FileInputStream(oldPath); //讀入原檔案
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1024];
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //位元組數 檔案大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
fs.close();
}
}
catch (Exception e) {
System.out.println("複製單個檔案操作出錯");
e.printStackTrace();
}
}
相關文章
- linux 帶路徑拷貝檔案Linux
- 檔案的相對路徑和絕對路徑以及根相對路徑
- html如何根據檔案路徑顯示檔案內容(pdf)HTML
- go掃描指定路徑下,檔案過多Go
- Eclipse-拷貝絕對路徑Eclipse
- windows mobile下的檔案路徑Windows
- 檔案絕對路徑和相對路徑
- Mac拷貝/複製資料夾路徑快捷鍵Mac
- Linux檔案的路徑定位-相對路徑和絕對路徑Linux
- js獲取專案根路徑JS
- 檔案路徑總結
- SQL Server 檔案路徑SQLServer
- 快速理解Android檔案儲存路徑Android
- JAVA 取得當前目錄的路徑/Servlet/class/檔案路徑/web路徑/url地址JavaServletWeb
- gcc g++ 新增標頭檔案路徑和庫檔案路徑的方法GC
- laravel 中根據請求路徑、方法匹配路由例項Laravel路由
- 改變歸檔檔案路徑
- 8.13 標頭檔案剖析:標頭檔案路徑(下)
- 使用RMAN複製活動資料庫(檔案路徑不同)資料庫
- 回顧Node檔案路徑
- 查詢檔案位置路徑
- redolog檔案更改路徑
- 修改資料檔案路徑
- JavaWeb中讀取【專案路徑下檔案】的路徑問題:this.getServletContext().getRealPath()JavaWebServletContext
- java中獲取類載入路徑和專案根路徑的5種方法Java
- [struts]當專案配置為根路徑時發生的action的路徑問題
- Java 輸出某路徑下的所有檔案Java
- android 把字串內容儲存到指定路徑Android字串
- python獲取指定目錄所有檔案絕對路徑Python
- Python最常用的讀取指定路徑檔案的方法!Python
- MySQL 指定各分割槽路徑MySql
- postfix按指定路徑安裝
- 初識TypeScript:查詢指定路徑下的檔案按型別生成jsonTypeScript型別JSON
- 非歸檔模式下的資料檔案路徑修改模式
- android 路徑大全Android
- 【Python】配置檔案配置路徑Python
- 檔案路徑問題( ./ 和 ../ 和 @/ )
- 017-html檔案路徑HTML