0

I'm trying to write a csv file on Android emulator. However, it shows "No such file or directory". How can I solve this problem?

How can I save the csv file on my computer?

Error:

W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Notes/realtive_angle.csv: open failed: ENOENT (No such file or directory)
W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:492)
W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:236)
W/System.err:     at java.io.FileWriter.<init>(FileWriter.java:107)
W/System.err:     at com.example.qz_app_v1.MainActivity.onCreate(MainActivity.java:57)
W/System.err:     at android.app.Activity.performCreate(Activity.java:7994)
W/System.err:     at android.app.Activity.performCreate(Activity.java:7978)
W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
W/System.err:     at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
W/System.err:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
W/System.err:     at android.os.Looper.loop(Looper.java:223)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7656)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
W/System.err:     at libcore.io.Linux.open(Native Method)
W/System.err:     at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
W/System.err:     at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
W/System.err:     at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7542)
W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:478)
W/System.err:   ... 18 more

Main code:

public class MainActivity extends AppCompatActivity {

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String csv = "realtive_angle.csv";
        double[] relative_x = {1,2,3};
        double[] relative_y = {4,5,6};
        double[] relative_z = {7,8,9};
        
        File root = new File(Environment.getExternalStorageDirectory(),"Notes");
        if (!root.exists()){
            root.mkdirs();
        }
        File gpxfile = new File(root, csv);
        try {
            FileWriter writer = new FileWriter(gpxfile,true);
//            CSVWriter writer = new CSVWriter(new FileWriter(csv));
            for(int i=0;i< relative_x.length;i++){
                String[] data = {Double.toString(relative_x[i]),Double.toString(relative_y[i]),Double.toString(relative_z[i])};
//                writer.writeNext(data);
                writer.append(data[0]);
                writer.append(data[1]);
            }
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
//            Toast.makeText(this,"Error while generating csv from data", Toast.LENGTH_SHORT).show();
        }
//        Object[] folderList = createFolderList_rev001();
    }
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Aria_Z
  • 13
  • 3
  • You cannot write to the root of external storage on Android 11+. Please use a location that you have read/write access to, such as via methods on `Context` like `getExternalFilesDir()`. – CommonsWare Jun 20 '22 at 20:58
  • Am i getting this right: You want to create a CSV on Android and save it on the emulator‘s host machine? – GabeRAMturn Jun 21 '22 at 08:20
  • Take a deep look at the official documentation of [Data and file storage overview](https://developer.android.com/training/data-storage). You really need to comprehend this doc when working with files. – Ricky Mo Jun 21 '22 at 08:24

1 Answers1

0

I think you don't have the permissions to make a file inside the storage. Check my answers here to have the solution to this problem:

LINK 1

LINK 2

Z3R0
  • 1,011
  • 10
  • 19