Android File Input/Output

Asked 2 years ago, Updated 2 years ago, 41 views

Hello,

While developing a notepad app for Android, we are trying to export what you wrote down in a file format.

The problem is that whether you specify a folder that exists in the device or I define a folder, the file is not created there. Please take a look and give me some advice.

First of all, build.gradel (Module:app) is as follows.

defaultConfig { applicationId "com.first.project.password" minSdkVersion 14 targetSdkVersion 24 versionCode 3 versionName "1.0.2" }

The device I'm running is less than API 22, so I'm proceeding with the permission setting specified in the manifest.

And if you call the following method in another class, the return value is false, The error message appears as follows, but I can't figure out where the problem is. First, the goal is to create the trythis.csv file and insert the string "abcd" into Environment.DIRECTORY_DOWNLOADS in getExternalStoragePublicDirectory.

"com.first.project E/com.first.project.DataExportImport: error : java.io.IOException: open failed: EACCES (Permission denied)"

public boolean getAlbumStorageDir() {
    try {
            String content= "trythis";
            String fpath = "/storage/emulated/0/myNewFolder/" + content + ".csv";
            File file = new File(fpath);
            // // If file does not exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write("abcd");
            bw.close();

            Log.d("Suceess", "Sucess");
            return true;

        } } catch (IOException e) {
            Log.e(TAG, "error : " + e.toString());
            return false;
        }
    }

I tried this and that, but I can't input and output files. Please give me some advice on which part is the problem.

public void getAlbumStorageDir() {
  // Method 1
        String content= "trythis.csv";
        File sdCard = Environment.getExternalStorageDirectory();
        File directory = new File(sdCard.getAbsolutePath() + content);

//2 method
    /*    /*    File file = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS), content);*/


        FileOutputStream outputStream;

        try {
            outputStream = new FileOutputStream(directory);
            outputStream.write(content.getBytes());
            outputStream.close();
        } } catch (IOException e) {
            Log.e(TAG, "error : " + e.toString() );
        }
    }

file io

2022-09-22 20:40

2 Answers

Version branching is easy to implement. For example, I think we can make it as follows. It's a simple example, so please refer to it.

@TargetApi(Build.VERSION_CODES.M)
public static boolean isPermissionGranted(Context context, String permission) {
    return Build.VERSION.SDK_INT < Build.VERSION_CODES.M
        || || context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
}

Where used

if (isPermissionGranted(context, Manifest.permission.ACCESS_FINE_LOCATION) {
    // Process existing logic if permissions are acquired
    ...
} } else {
    // Invoke permission request function
    ...

}


2022-09-22 20:40

If targetSdkVersion >= 23, the permission must be obtained at runtime. (In the code you uploaded, the targetSdkVersion is set to 24) Please lower the targetSdkVersion to 22 or process the runtime permission.


2022-09-22 20:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.