【问题标题】:Saving a file into mounted internal storage and downloading it from PC将文件保存到已安装的内部存储中并从 PC 下载
【发布时间】:2015-12-08 13:06:45
【问题描述】:

我有一个生成大输出文件的应用程序。我将它们保存到内部存储的“下载”文件夹中,我想从我电脑上安装的手机存储的“下载”文件夹中检索它们。

我使用的代码包含:

String filename="output.txt";
File dirname = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
FileWriter outFileWriter;
outFile = new File(dirname, filename);
outFileWriter = new FileWriter(outFile);
outFileWriter.append(outputString);
outFileWriter.close();

手机是一加2,文件夹在我的Windows 7上安装为Computer\ONE A2001\Internal storage\Download

从 adb shell 我可以看到并读取文件,但计算机看不到它。当我将文件从我的计算机复制到已安装的下载文件夹中时,我可以从 adb shell 中看到它,所以我知道我正在寻找一个正确的位置,但计算机看不到我的应用程序的输出文件。

ls -l 给出:

shell@OnePlus2:/storage/sdcard0/Download $ ls -l
ls -l
-rw-rw---- root     sdcard_r  2030918 2015-09-11 17:24 copiedFromComputer.jpg
-rw-rw---- root     sdcard_r   581632 2015-09-11 17:32 output.txt

所以文件权限是一样的,但是为什么我在电脑上看不到我的output.txt文件呢?

附言。有没有更好的替代方法,可以在不使用 adb 备份的情况下将应用生成的文件检索到 PC 中?

【问题讨论】:

    标签: android file internal-storage


    【解决方案1】:

    好的,显然文件在创建后需要由媒体扫描仪编制索引,以便可以访问。

    http://developer.android.com/reference/android/os/Environment.html 可以在应用程序中调用:

    // Tell the media scanner about the new file so that it is
    // immediately available to the user.
    MediaScannerConnection.scanFile(this,
                new String[] { file.toString() }, null,
                new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
                Log.i("ExternalStorage", "Scanned " + path + ":");
                Log.i("ExternalStorage", "-> uri=" + uri);
            }
        });
    

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(outFile)));
    

    或来自 adb shell:

    shell@shamu:/ $ am broadcast android.intent.action.MEDIA_MOUNTED
    

    但是,如果文件在被编入索引后被修改,则更改不会反映到其元数据中,例如文件大小。但是,如果您将其下载到您的计算机,大小和其他元数据将得到更正。

    也许有人可以评论如何在Android系统上修改后更新已索引文件的元数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-19
      • 2017-12-05
      • 1970-01-01
      • 2017-07-12
      • 1970-01-01
      • 2021-04-16
      • 2021-05-17
      相关资源
      最近更新 更多