[英]File not found Exception while copying Android File
我正在嘗試使用此代碼。
public synchronized void onActivityResult(final int requestCode, int resultCode,
final Intent data) {
String filePath = null;
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case RESULT_MEMORY_SAVE:
filePath = data.getDataString();
copyFileForReceipts(filePath,pathOfReceipts);
現在filePath是一個String,調試器將其值顯示為"content://media/external/images/media/133"
接下來,我嘗試使用此:
sourceLocation = filePath;
File afile = new File(sourceLocation);
File bfile = new File(targetLocation + "file.jpg");
inStream = new FileInputStream(afile.getName()); //Gives exception
outStream = new FileOutputStream(bfile.getName());
它給出FileNotFound
異常。 我也嘗試過刪除.getName()
。
請告訴我我錯了。
問候
而不是使用setData和getDataString ,應該放置文件路徑並按以下方式檢索它:
在結果生成活動中添加路徑:
intent.putString("path", file_path);
並獲取路徑:
filePath = data.getString("path");
copyFileForReceipts(filePath,pathOfReceipts);
在進一步處理文件之前先檢查文件是否存在是一個好習慣:
if(afile.exists()){
//do whatever you want
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.