【问题标题】:Need help writing file to external storage需要帮助将文件写入外部存储
【发布时间】:2017-02-08 07:01:44
【问题描述】:

我已经在互联网上搜索了几个小时,试图找到一个真正适合我的文件写入功能。到目前为止,我的 MainActivity.java 中有这个:

public void writeToFile(String data, Context ctx) {
    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
    File tempFile = new File(path, "houseDataFile.txt");
    if(!tempFile.exists()){
        tempFile.mkdirs();
    }
    try
    {
        FileOutputStream fOut = new FileOutputStream(tempFile);
        fOut.write(data.getBytes());
        fOut.close();
        Toast.makeText(ctx, "Saved", Toast.LENGTH_SHORT).show();
    }catch (Exception e)
    {
        Log.w(TAG, "FileOutputStream exception: - " + e.toString());
    }
}

我的 android manifest 包含这两个权限:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="my.package.application"xmlns:android="http://schemas.android.com/apk/res/android">

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

<application

    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">

    </activity>
    <activity android:name=".Menu_activity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

虽然当我运行应用程序并调用 writeToFile() 时相当令人沮丧,但它给了我一个错误提示:

W/MainActivity: FileOutputStream exception: - java.io.FileNotFoundException: /storage/emulated/0/Documents/houseDataFile.txt: open failed: ENOENT (No such file or directory)

任何帮助将不胜感激,在此先感谢您。

【问题讨论】:

  • /storage/emulated/0/Documents 存在吗?
  • 您设备的安卓版本是否 >= 6.x.x (Marsh Mallow) ?
  • been searching the internet for several hours now。如果您阅读过一些标记为 android 的 stackoverflow 页面,您会发现您的问题被多次报告。每周几次。

标签: java android file io


【解决方案1】:

我假设您无法获取文件路径,因为您在执行方法时没有询问运行时权限。在这里你可以看看下面的代码。

首先,检查您设备的操作系统版本是否高于 Lollipop。如果以上则显示权限弹出对话框。

final int MyVersion = Build.VERSION.SDK_INT;
if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1)
{
    if (!checkIfAlreadyhavePermission())
    {
        ActivityCompat.requestPermissions(ProfileActivity.this, new String[] {android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
    }
    else
    {
        writeToFile(data, ctx);
    }
}
else
{
    writeToFile(data, ctx)
}

 private boolean checkIfAlreadyhavePermission() {
    int result = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
    return result == PackageManager.PERMISSION_GRANTED;
}

然后在 OnRequestPerMissionResult() 中定义“允许”和“拒绝”按钮功能。

 public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 1: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    writeToFile( data, ctx);

                } else {
                    Toast.makeText(YourActivity.this, "Please provide permission", Toast.LENGTH_LONG).show();
                }
                break;
            }
    }

如果这能解决问题,请告诉我。

【讨论】:

  • 现在我收到错误:FileOutputStream exception: - java.io.FileNotFoundException: /storage/emulated/0/Documents/houseDataFile.txt: open failed: EISDIR (Is a directory)。我将尝试写入内部存储,看看是否有帮助。
  • 您是否也尝试过使用 WRITE PERMISSION ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-28
  • 2011-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-12
  • 1970-01-01
相关资源
最近更新 更多