简体   繁体   中英

canRead() and canWrite() both return false

I'm somehow not able to read or write files. I'm using the following to get a filename. It's probably something obvious to most, but my Java is a bit weak.

private String fileName(int itemNum) {
    final String DIR_PREFIX = "/MyDailySelfiesDir";
    final String FILENAME_PREFIX = "/MyDailySelfieFile_";
    File dir_0 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    String dirName = dir_0.getAbsolutePath() + DIR_PREFIX;
    File dir = new File(dirName);
    dir.mkdirs();
    String name = dirName + FILENAME_PREFIX + itemNum + ".jpg";
    File temp = new File(name);
    Log.i(TAG, name + " can read? " + temp.canRead() + " can write? " + temp.canWrite());
    return name;
}

AndroidManifest.xml looks like this, in part:

<uses-sdk
    android:minSdkVersion="18"
    android:targetSdkVersion="19" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

I'm in Android Studio, emulating a Galaxy Nexus 5, API 18.

Try checking the value of yourFile.exists(). Unless you have not posted all the code, you never actually create your File 'temp' on your device - you have only created a File object. thus you can't read or write to the File object if it doesn't map to a real file in the Android file system.. You can create a file with File.createNewFile().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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