簡體   English   中英

找不到文件復制Android文件時出現異常

[英]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()

請告訴我我錯了。

問候

而不是使用setDatagetDataString ,應該放置文件路徑並按以下方式檢索它:

在結果生成活動中添加路徑:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM