Save file on google drive

920 Views Asked by At

Hey I'm trying do application where i check my current location and save this location in ".txt" files. My application save this location in every "user set time" seconds. And it's work. Also I want add save files to Google drive. But I don't know how. Is there any method by which I can create a folder and save ".txt" files same as i did with local folder?

public class Save extends AppCompatActivity {
    boolean sms = false;
    int n_seconds, n_minutes, n_sum;
    private String path = Environment.getExternalStorageDirectory().toString() + "/Loc/Save";
    private Button buttonStartThread;
    private Handler mainHandler = new Handler();
    private volatile boolean stopThread = false;
    NumberPicker edit_text_input_back, edit_text_input_back_2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_background);
        buttonStartThread = findViewById(R.id.button_start_thread);

        edit_phone_number = findViewById(R.id.edit_phone_number);
        edit_mail = findViewById(R.id.edit_email);
        edit_text_input_back = (NumberPicker) findViewById(R.id.edit_text_input_back);
        edit_text_input_back.setMaxValue(60);
        edit_text_input_back.setMinValue(0);
        edit_text_input_back.setValue(0);
        edit_text_input_back_2 = (NumberPicker) findViewById(R.id.edit_text_input_back_2);
        edit_text_input_back_2.setMaxValue(60);
        edit_text_input_back_2.setMinValue(0);
        edit_text_input_back_2.setValue(0);
        edit_text_input_back.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int i, int i1) {
            n_seconds = i1;
            }
        });
        edit_text_input_back_2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int i, int i1) {
                n_minutes = 60 * i1;
            }
        });

    }

    public void startThread(View view) {

        stopThread = false;
        n_sum = n_seconds + n_minutes;

        ExampleRunnable runnable = new ExampleRunnable(n_sum);
        new Thread(runnable).start();
        buttonStartThread.setEnabled(false);

    }
    public void stopThread(View view) {
        stopThread = true;
        buttonStartThread.setEnabled(true);
    }

    class ExampleRunnable implements Runnable {
        int seconds;
        ExampleRunnable(int seconds) {
            this.seconds = seconds;
        }
        @Override
        public void run() {
            for (; ; ) {
                for (int i = 0; i < seconds; i++) {
                    if (stopThread)
                        return;
                    if (i == n_sum-1) {

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                
                                    createDir();
                                    createFile();
                                                                
                                }


                        });
                    }
                    Log.d(TAG, "startThread: " + i);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        }

    private void createDir() {
        File folder = new File(path);
        if(!folder.exists()){
            try {
                folder.mkdirs();
            }catch (Exception e){
                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
            }
        }

    }
    private void createFile() {
        File file = new File(path+"/"+System.currentTimeMillis()+".txt");
        FileOutputStream fileOutputStream;
        OutputStreamWriter outputStreamWriter;
        try {
            Intent intent = getIntent();
            Double lat = intent.getDoubleExtra("adres", 0);
            Double lon = intent.getDoubleExtra("adres2", 0);
            String adr = intent.getStringExtra("adres3");
            fileOutputStream = new FileOutputStream(file);
            outputStreamWriter = new OutputStreamWriter(fileOutputStream);
            outputStreamWriter.append("Your adress: " + adr + ". " + "Your latitude: " + lat + ", " + "longtitude: " + lon+".");
            outputStreamWriter.close();
            fileOutputStream.close();
        }catch (Exception e){
            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
        }
    }

    }

}

Thank you in advance

1

There are 1 best solutions below

2
On

There are ample sources you can check for creating a folder, and uploading a file to Google Drive

Creating Folder

File fileMetadata = new File();
fileMetadata.setName("Invoices");
fileMetadata.setMimeType("application/vnd.google-apps.folder");

File file = driveService.files().create(fileMetadata)
    .setFields("id")
    .execute();
System.out.println("Folder ID: " + file.getId());

Uploading File

File fileMetadata = new File();
fileMetadata.setName("photo.jpg");
java.io.File filePath = new java.io.File("files/photo.jpg");
FileContent mediaContent = new FileContent("image/jpeg", filePath);
File file = driveService.files().create(fileMetadata, mediaContent)
    .setFields("id")
    .execute();
System.out.println("File ID: " + file.getId());

For the specific mime type of a txt file, you might need to refer this StackOverflow post (text/plain). For specific Google applications mime types, please see documentation.