简体   繁体   English

Android写入文件 - 找不到文件

[英]Android write to file - can't find file

I am a newbie in Android development. 我是Android开发的新手。 I try to write something to a file. 我尝试写一些文件。 I am using the code: 我正在使用代码:

try {
                    FileOutputStream fOut = openFileOutput(
                            "out.txt", MODE_WORLD_READABLE);

                    OutputStreamWriter osw = new OutputStreamWriter(fOut);
                    osw.write("something");
                    osw.flush();
                    osw.close();
                    fOut.close();

                } catch (MalformedURLException e) {
                    Log.e("FILE WRITE ERROR", e.getMessage());
                } catch (IOException e) {
                    Log.e("FILE WRITE ERROR", e.getMessage());
                }

Everything it's ok but I can't find the file in DDMS FIle Explorer. 一切都没问题,但我在DDMS FIle Explorer中找不到该文件。 Could you help me and tell me where Android saves the file? 你能帮助我并告诉我Android保存文件的位置吗? It isn't in "/data/data/my_project_package". 它不在“/ data / data / my_project_package”中。 Thank you. 谢谢。

1) be sure to have this in your manifest: 1)一定要在你的清单中有这个:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

2) It makes a difference, either testing with the emulator or the device. 2)无论是使用仿真器还是使用设备进行测试都会产生影响。 In case of the latter, be sure to unmount the device from your PC (Pick: "Only charging"). 如果是后者,请务必从PC上卸下设备(选择:“仅充电”)。 Debugging will still work that way. 调试仍然会以这种方式工作。 It's just that you cannot write and watch at the same time. 只是你不能同时写作和观看。

This method works: 此方法有效:

public void writeToExternalStoragePublic(String filename, int content) {
    String packageName = this.getPackageName();
    String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/Android/data/" + packageName + "/files/";
    if (isExternalStorageAvailable() && !isExternalStorageReadOnly()) {
        try {
            boolean exists = (new File(path)).exists();
            if (!exists) {
                new File(path).mkdirs();
            }
            // Open output stream
            FileOutputStream fOut = new FileOutputStream(path + filename,true);
            // write integers as separated ascii's
            fOut.write((Integer.valueOf(content).toString() + " ").getBytes());
            fOut.write((Integer.valueOf(content).toString() + " ").getBytes());
            // Close output stream
            fOut.flush();
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

There seems to be a lot of questions on this topic and the problem often seems to boil down to i) not requesting permissions in the AndroidManifest.xml ii) The device's storage being mounted to a computer at the time the app was being run or iii) emulator settings. 关于这个主题似乎有很多问题,问题通常似乎归结为i)不在AndroidManifest.xml请求权限ii)设备的存储在应用程序运行时被安装到计算机或iii )模拟器设置。

In the hope that others who have tried all these solutions and failed, I submit iv) computer decides to arbitrarily ignore your permission setting in AndroidManifest.xml after pulling code from a source repository that was written on another computer. 希望其他尝试过所有这些解决方案并失败的人,我提交iv)计算机决定在从另一台计算机上写入的源存储库中提取代码后,在AndroidManifest.xml任意忽略您的权限设置。 In my case I'd just written some code on Computer A that wrote to external storage. 在我的情况下,我只是在计算机A上写了一些写入外部存储器的代码。 I checked said code into source control and then pulled said code to Computer B. When I reinstalled the app from Computer B onto the device, I started getting the Permission denied exception. 我将所述代码检查到源代码控制中,然后将所述代码拉到计算机B.当我将计算机B中的应用程序重新安装到设备上时,我开始获得Permission denied异常。

The fix : make any change to the AndroidManifest.xml file on Computer B (eg insert a newline somewhere). 修复 :对计算机B上的AndroidManifest.xml文件进行任何更改(例如,在某处插入换行符)。 This fixed the problem for me. 这解决了我的问题。 I don't know why. 我不知道为什么。 It's probably one of the rarer cases, but more frustrating ones. 这可能是最罕见的情况之一,但更令人沮丧。

Place <uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE"> in your manifest. 在您的清单中放置<uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE">

Here is a reference on it. 是一个参考。

I found that I didn't need to change the USB option to "Only Charging" and could keep it in "file transfer" mode. 我发现我不需要将USB选项更改为“仅充电”并且可以将其保持在“文件传输”模式。 However, I did need to call a method to the Android Media Scanner to display this file. 但是,我确实需要调用Android Media Scanner的方法来显示该文件。

How to Run Media Scanner in Android 如何在Android中运行Media Scanner

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM