簡體   English   中英

手機SD卡連接到電腦時文件不可見

[英]File is not visible when phone sd card is connected to pc

我從android創建了一個excel文件,當我將手機連接到計算機,然后在計算機中我無法看到該文件。 這是為什么?

    Workbook wb = new HSSFWorkbook();
    Cell c = null;

    //Cell style for header row
    CellStyle cs = wb.createCellStyle();

    //New Sheet
    Sheet sheet1 = null;
    sheet1 = wb.createSheet("Meter Readings");

    // Generate column headings
    Row row = sheet1.createRow(1);

    c = row.createCell(0);
    c.setCellValue("Flat Number");
    c.setCellStyle(cs);

    sheet1.setColumnWidth(0, (15 * 200));

    // Create a path where we will place our List of objects on external storage
    File file = new File(context.getExternalFilesDir(null),fileName);
    FileOutputStream os = null;

    try {
        os = new FileOutputStream(file);
        wb.write(os);
        Log.w("FileUtils", "Writing file" + file);
        success = true;
        //os.close();
    } catch (IOException e) {
        Log.w("FileUtils", "Error writing " + file, e);
    } catch (Exception e) {
        Log.w("FileUtils", "Failed to save file", e);
    } finally {
        try {
            if (null != os) {
                os.close();
            }
        } catch (Exception ex) {
        }
    }

您需要重新啟動手機或重新連接到PC。

可能缺乏寫入文件的權限

WRITE_EXTERNAL_STORAGE
READ_EXTERNAL_STORAGE

它也可能是未安裝外部存儲器,請檢查:

Environent.getExternalStorageState();

PC的文件瀏覽器沒有立即刷新Android文件系統。

所以我建議你使用adb shell命令。 它會立即顯示設備文件系統的更改。

  1. $ adb devices //獲取您的設備ID。 如果僅連接一個,則不需要此命令。

  2. $ adb shell (-s your-device-id) //輸入設備的文件系統。

  3. shell$ cd sdcard/ //通常情況下,默認設備存儲的路徑是/sdcard 更改目錄,然后找到您的文件。

  4. shell$ exit //如果找到該文件,請退出adb shell

  5. $ adb pull /sdcard/<parent-directory-path>/<file-name> <destination-path-on-your-PC> //如果需要在系統上獲取此文件,請使用此命令。

如果找不到,則可能尚未創建該文件。 在這種情況下,您將檢查項目設置如下:

  1. 如果您的應用程序支持API 19,請在AndroidManifest.xml設置<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

  2. 如果您的應用程序支持API 21,請實施RuntimePermission例程。

    根據此頁面 ,當應用程序在Android 6.0或更高版本上運行時,將授予權限。

    要實現,請查看此文章以參考示例代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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