Home > other >  How to write csv file on Android emulator?
How to write csv file on Android emulator?

Time:06-21

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();
    }

CodePudding user response:

Maybe Your emulator android version is 11 or above there for you can't create folder in root directory. You can use scopped storage for this. Or You can use Storage Access Framework to save file in root directory.

CodePudding user response:

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

  • Related