Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我的应用程序创建的文件不是';t添加到可见文件系统_Android_File_Filesystems - Fatal编程技术网

Android 我的应用程序创建的文件不是';t添加到可见文件系统

Android 我的应用程序创建的文件不是';t添加到可见文件系统,android,file,filesystems,Android,File,Filesystems,以下是我正在运行的代码: File root = android.os.Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); File dir = new File(root.getAbsolutePath()); dir.mkdirs(); File file = new File(dir, "myData.txt");

以下是我正在运行的代码:

        File root = android.os.Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

        File dir = new File(root.getAbsolutePath());
        dir.mkdirs();
        File file = new File(dir, "myData.txt");

        try {
            FileOutputStream f = new FileOutputStream(file);
            PrintWriter pw = new PrintWriter(f);
            pw.println("Hi , How are you");
            pw.println("Hello");
            pw.flush();
            pw.close();
            f.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            android.util.Log
                    .i("tag",
                            "******* File not found. Did you add a WRITE_EXTERNAL_STORAGE permission to the   manifest?");
        } catch (IOException e) {
            e.printStackTrace();
        } 
代码执行时不会出现问题,如果我运行这些测试,所有测试都会显示文件系统中存在该文件:

        File testFile = new File(dir, "myData.txt");
        Boolean exists = testFile.exists();//true
        Boolean hidden = testFile.isHidden();//false
        String[] files = dir.list();//list contains the file
但是我在文件资源管理器、根浏览器或终端模拟器中看不到该文件。而
dir.list()
查询的文件列表与我在根浏览器中看到的
/mnt/sdcard/Downloads
不同。我尝试在终端仿真器中用
find/-name*myData*
搜索文件
myData.txt
,但没有结果

我在根s4上运行代码,AndroidManifest包含WRITE\u EXTERNAL\u存储权限


为什么会发生这种情况?我如何让用户在创建文件后查看该文件?

可能您没有使用
MediaScannerConnection
对其进行索引,或者您的计算机缓存了MTP连接到设备的结果。

可能您没有使用
MediaScannerConnection
对其进行索引,或者,您的计算机可能会缓存MTP连接到设备的结果。

我的活动:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    File root = android.os.Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

    File dir = new File(root.getAbsolutePath());
    dir.mkdirs();
    File file = new File(dir, "myData.txt");

    try {
        FileOutputStream f = new FileOutputStream(file);
        PrintWriter pw = new PrintWriter(f);
        pw.println("Hi , How are you");
        pw.println("Hello");
        pw.flush();
        pw.close();
        f.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
        android.util.Log
                .i("tag",
                        "******* File not found. Did you add a WRITE_EXTERNAL_STORAGE permission to the   manifest?");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.Test.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    File root = android.os.Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

    File dir = new File(root.getAbsolutePath());
    dir.mkdirs();
    File file = new File(dir, "myData.txt");

    try {
        FileOutputStream f = new FileOutputStream(file);
        PrintWriter pw = new PrintWriter(f);
        pw.println("Hi , How are you");
        pw.println("Hello");
        pw.flush();
        pw.close();
        f.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
        android.util.Log
                .i("tag",
                        "******* File not found. Did you add a WRITE_EXTERNAL_STORAGE permission to the   manifest?");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.Test.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

输出:
我的活动:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    File root = android.os.Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

    File dir = new File(root.getAbsolutePath());
    dir.mkdirs();
    File file = new File(dir, "myData.txt");

    try {
        FileOutputStream f = new FileOutputStream(file);
        PrintWriter pw = new PrintWriter(f);
        pw.println("Hi , How are you");
        pw.println("Hello");
        pw.flush();
        pw.close();
        f.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
        android.util.Log
                .i("tag",
                        "******* File not found. Did you add a WRITE_EXTERNAL_STORAGE permission to the   manifest?");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.Test.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    File root = android.os.Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

    File dir = new File(root.getAbsolutePath());
    dir.mkdirs();
    File file = new File(dir, "myData.txt");

    try {
        FileOutputStream f = new FileOutputStream(file);
        PrintWriter pw = new PrintWriter(f);
        pw.println("Hi , How are you");
        pw.println("Hello");
        pw.flush();
        pw.close();
        f.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
        android.util.Log
                .i("tag",
                        "******* File not found. Did you add a WRITE_EXTERNAL_STORAGE permission to the   manifest?");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.Test.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

输出:

此帖子的可能副本与许可无关此帖子的可能副本与许可无关ID尝试MediaScannerConnection,无效(将apk复制到手机并手动安装。仍然无效(尝试MediaScannerConnection,无效(将apk复制到手机并手动安装。仍然无效(@ArsenZahray Welcome.happy coding dude:)@ArsenZahray Welcome.happy coding dude:)