2014-11-04 59 views
1

am創建Excel文件將其存儲在內部存儲器中,但無法完成。它僅在應用程序存儲目錄內創建。不能在public中顯示。如何創建文件夾並將文件存儲在該文件夾中?任何人都可以幫助我解決這個問題。公開在內部存儲中添加文件夾?

文件創建編碼

public String generate(String file_name,String path) { 

    try { 

     f = new File(activity.getFilesDir(), path); 
     if (!f.exists()) { 
      f.mkdirs(); 
     } 


     file = new File(f.getAbsolutePath(), file_name); 
     if (file.createNewFile()) { 
      file.createNewFile(); 
     } 

     wb_setting = new WorkbookSettings(); 
     wb_setting.setLocale(new Locale("en", "EN")); 

     workbook = Workbook.createWorkbook(file, wb_setting); 
     workbook.createSheet("Report", 0); 
     excelSheet = workbook.getSheet(0); 
     createLabel(excelSheet); 
     createContent(excelSheet); 

     workbook.write(); 
     workbook.close(); 

     file_path_alert_builder = new AlertDialog.Builder(activity); 
     file_path_alert_builder.setTitle("File path"); 
     file_path_alert_builder.setMessage(""+file).setCancelable(true).setPositiveButton("OK",new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 

       dialogInterface.dismiss(); 

      } 
     }); 

     file_path_dialog = file_path_alert_builder.create(); 
     file_path_dialog.show(); 
    }catch (JXLException jxl_e) { 
     jxl_e.printStackTrace(); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return null; 
} 

回答

1

您必須選擇存儲文件的正確路徑。有多種選擇

內部存儲

  • 內部的應用程序(從外側端用戶不能訪問)
  • 緩存目錄
(也可以,如果系統運行的空間被清除)

外部存儲(驗證它是否可用並使用它)雖然它是公開的,但有2種類型

  • public
  • 私有(技術上可由用戶和其他應用程序訪問,因爲它們位於外部存儲上,它們是實際上不會爲應用程序外的用戶提供價值的文件。 )

每個路徑位置都可以通過android提供的不同API訪問。請參閱http://developer.android.com/training/basics/data-storage/files.html

+0

好吧,但一些流行的應用程序,如什麼,Viber,camscanner應用程序有文件夾來顯示文件在內部存儲。 – Yugesh 2014-11-04 09:55:37

+0

他們使用外部存儲。可以使用getExternalFilesDir()。 – Aun 2014-11-04 10:04:31

0

您看到的意思是什麼公開?由其他應用程序訪問?如果是這種情況,請使用: getExternalFilesDir()改爲

+0

當用戶打開內部存儲器時意味着文件夾將可見。 – Yugesh 2014-11-04 09:18:41

+0

然後你應該使用由@Aun建議的'Environment.getExternalStoragePublicDirectory'。請注意,「外部存儲」可以是一個可移動存儲介質(如SD卡)或內部(不可移動)存儲 – 2014-11-04 09:48:30

相關問題