简体   繁体   English

无法创建文件

[英]File can't be created

I am creating file in the external storage and adding values to that file, the problem is, although i added the required permissions, at run time i receive the below posted log errors . 我正在external storage创建文件并向该文件中添加值,问题是,尽管我添加了必需的权限,但在运行时我收到以下发布的日志错误

also when i connect the device "Asus nexus 7" to the USB, i can't see any contents on the device, I mean the device is connected, but I am not able to browse any files in the device . 同样,当我将设备"Asus nexus 7"连接到USB时,我看不到设备上的任何内容,这意味着设备已连接,但无法浏览设备中的任何文件

NOTE: 注意:

the same code creates the files normally when Samsung Galaxy note is   
connected, but when i connect Asus Nexus7 i receive the below errors plus 
the storage of the device cant be seen when connected.

code : 代码

12-04 10:44:43.213 4551-4551/com.example.com.myapplication W/IOCtrl: +++++ createFile() +++++
12-04 10:44:43.213 4551-4551/com.example.com.myapplication W/IOCtrl: +++++ isExternalStorageMounted() +++++
12-04 10:44:43.218 4551-4551/com.example.com.myapplication D/IOCtrl: isExternalStorageMounted(): -> media state: mounted
12-04 10:44:43.218 4551-4551/com.example.com.myapplication D/IOCtrl: dir: /storage/emulated/0/CAN_BUS already exists
12-04 10:44:43.218 4551-4551/com.example.com.myapplication D/IOCtrl: file: log0.txt will be created
12-04 10:44:43.219 4551-4551/com.example.com.myapplication W/System.err: java.io.IOException: open failed: EACCES (Permission denied)
12-04 10:44:43.219 4551-4551/com.example.com.myapplication W/System.err:     at java.io.File.createNewFile(File.java:939)
12-04 10:44:43.219 4551-4551/com.example.com.myapplication W/System.err:     at com.example.com.myapplication.IOCtrl.createFile(IOCtrl.java:86)
12-04 10:44:43.219 4551-4551/com.example.com.myapplication W/System.err:     at com.example.com.myapplication.MainActivity$1.onClick(MainActivity.java:66)
12-04 10:44:43.219 4551-4551/com.example.com.myapplication W/System.err:     at android.view.View.performClick(View.java:5198)
12-04 10:44:43.219 4551-4551/com.example.com.myapplication W/System.err:     at android.view.View$PerformClick.run(View.java:21147)
12-04 10:44:43.219 4551-4551/com.example.com.myapplication W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
12-04 10:44:43.219 4551-4551/com.example.com.myapplication W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
12-04 10:44:43.219 4551-4551/com.example.com.myapplication W/System.err:     at android.os.Looper.loop(Looper.java:148)
12-04 10:44:43.219 4551-4551/com.example.com.myapplication W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
12-04 10:44:43.219 4551-4551/com.example.com.myapplication W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
12-04 10:44:43.219 4551-4551/com.example.com.myapplication W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-04 10:44:43.219 4551-4551/com.example.com.myapplication W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-04 10:44:43.219 4551-4551/com.example.com.myapplication W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
12-04 10:44:43.220 4551-4551/com.example.com.myapplication W/System.err:     at libcore.io.Posix.open(Native Method)
12-04 10:44:43.220 4551-4551/com.example.com.myapplication W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
12-04 10:44:43.220 4551-4551/com.example.com.myapplication W/System.err:     at java.io.File.createNewFile(File.java:932)
12-04 10:44:43.220 4551-4551/com.example.com.myapplication W/System.err:    ... 11 more
12-04 10:44:43.220 4551-4551/com.example.com.myapplication E/IOCtrl: <<createFile>>:  Error creating file: open failed: EACCES (Permission denied)

Update::code : 更新::代码

//how the file is being created

public class IOCtrl {

private final static String TAG = IOCtrl.class.getSimpleName();

private static final String COMMA = ",";
private static final String NAN = "NAN";
private static final String NOLOC = "NO_LOC";
private static final String NEW_LINE = System.lineSeparator();

private final static String DIR_NAME = "CAN_BUS";
private final static String ROOT_DIR = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();

private static boolean isExternalStorageMounted() {
    Log.w(TAG, CSubTag.msg("isExternalStorageMounted"));

    String state = Environment.getExternalStorageState();
    boolean mediaAvailable = false;

    Log.d(TAG, CSubTag.msg("isExternalStorageMounted", "media state: " + state));

    switch (state) {

        case Environment.MEDIA_MOUNTED:
            state = Environment.MEDIA_MOUNTED;
            mediaAvailable = true;
            break;

        case Environment.MEDIA_MOUNTED_READ_ONLY:
            state = Environment.MEDIA_MOUNTED_READ_ONLY;
            mediaAvailable = true;
            break;

        default:
            mediaAvailable = false;
            break;
    }

    return mediaAvailable;
}


public static File createFile(String fileName) {
    Log.w(TAG, CSubTag.msg("createFile"));

    String state;

    if (IOCtrl.isExternalStorageMounted()) {
        //Log.v(TAG, CSubTag.subBullet("createFile", "MEDIA_MOUNTED_READ_ONLY"));

        File dir = new File(IOCtrl.ROOT_DIR + File.separator + IOCtrl.DIR_NAME);
        boolean dirCreated = dir.mkdirs();

        if (dirCreated) {
            Log.d(TAG, "dir: " + dir.getAbsolutePath() + " created");
        } else {
            Log.d(TAG, "dir: " + dir.getAbsolutePath() + " already exists");
        }

        File file = new File(dir, fileName);
        boolean fileExists = file.exists();

        if (fileExists) {
            Log.d(TAG, "file: " + fileName + " already exists");
        } else {
            Log.d(TAG, "file: " + fileName + " will be created");
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
                Log.e(TAG, CSubTag.subBullet("createFile", "Error creating file: " + e.getMessage()));
                return null;
            }
        }

        return file;
    } else {
        Log.e(TAG, CSubTag.msg("createFile", "media storage is not available"));
        return null;
    }

}
}
> open failed: EACCES (Permission denied)
you can only write on public folder. try to store your file on Download folder

 File file; file=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

use permission in manifest at right place. 在清单的正确位置使用许可。

    <application>
        ...

    </application>

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

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

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