0

I wanted to create a file in the internal directory of the Android and I'm unable to create it despite the standard procedure to create a file and a directory it. And when I install my app in my phone I'm not able to find the "com.package..." folder, installation done through direct android studio connected.

import android.os.Environment;
import android.util.Log;
import android.widget.TextView;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;


public class CreateLogObject {

    public static final String TAG = "CreateLogObject ";
    private String logId;
    private TextView GPScurrentSpeed;
    private TextView latitude;
    private TextView longitude;
    private TextView accur;
    private TextView fomulaSpd;
    private String Xval;
    private String Yval;
    private String Zval;

    public CreateLogObject(String logId, TextView latitude, TextView longitude, TextView accur, TextView GPScurrentSpeed, TextView forSpeed, String X, String Y, String Z) {
        this.logId = logId;
        this.GPScurrentSpeed = GPScurrentSpeed;
        this.latitude = latitude;
        this.longitude = longitude;
        this.accur = accur;
        this.fomulaSpd = forSpeed;
        this.Xval = X;
        this.Yval = Y;
        this.Zval = Z;
        logDetails();
    }

    @Override
    public String toString() {
        return  ",  " + logId + ",  " + latitude.getText() + ", " + longitude.getText() + "," + accur.getText() + ",  getSpeed() Velocity:"
                + GPScurrentSpeed.getText() + ", Formula Speed:" + fomulaSpd.getText() + " , X: " + Xval + " , Y: " + Yval + " , Z: " + Zval;
    }

    private void logDetails() {

        generateNoteOnSD("speedLog.txt", toString());
    }

    public void generateNoteOnSD(String sFileName, String sBody) {

        try {
            File root = new File(Environment.getExternalStorageDirectory(), "LogFolder");
            if (!root.exists()) {
                Log.d(TAG, "MISSING DIRECTORY CREATED");
                root.mkdirs();
            }
            File gpxfile = new File(root, sFileName);
            FileWriter writer = new FileWriter(gpxfile, true);
            writer.append(new StringBuffer(sBody).append("\n"));
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

And yes I have give permissions to WRITE and READ into internal/external directory. In need of immediate help please do guide with your suggestions.

  • https://stackoverflow.com/questions/32789157/how-to-write-files-to-external-public-storage-in-android-so-that-they-are-visibl – CommonsWare May 25 '17 at 19:45
  • `Log.d(TAG, "MISSING DIRECTORY CREATED` No. You are to early. And mkdirs() can fail. Check the return value before you shout so loud. – greenapps May 25 '17 at 20:19
  • So you are unable to create a directory to begin with. You should not even try to create a file in a directory that does not exist. – greenapps May 25 '17 at 20:20
  • @greenapps I tried without creating directory and failed and had the same issue with it. – VinyasBabu May 26 '17 at 11:20
  • And @greenapps could you please say of why "com.package...." is not being created when app is being installed? – VinyasBabu May 26 '17 at 11:27
  • I see no com.package.... Where are you talking about? Ok. How are you searching for it? Confusing you have two problems in one post. Dont do such things please. Please adapt your code with my suggestion. – greenapps May 26 '17 at 11:36
  • `I tried without creating directory` ?? Of course you should try to create the directory if it does not exist. Unclear what you mean. Please adapt your code. Of course i know that mkdirs() fails. But you should adapt your code to handle failing. – greenapps May 26 '17 at 11:40
  • @greenapps, I'm asking regarding the folder created after the app is installed into the phone. And I would stick to 1 question from next time on. But the reason I put it here becoz I read that the files implicitly gets created in "com.package..../data/" folder when just told to write a file in internal memory. Please do correct me if I'm wrong in my understanding in any of the points. – VinyasBabu May 26 '17 at 13:34
  • Lets first bring the first question to an end. You have not reacted to the things i said. No fun. – greenapps May 26 '17 at 14:08

0 Answers0