在google驱动器上保存文件

xuo3flqw  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(460)

嘿,我正在尝试做的应用程序,我检查我的当前位置,并保存在“.txt”文件这个位置。我的应用程序在每个“用户设置时间”秒内保存此位置。这是工作。我还想添加保存文件到谷歌驱动器。但我不知道怎么做。有什么方法可以让我创建一个文件夹并保存“.txt”文件,就像我使用本地文件夹一样吗?

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

    }

}

先谢谢你

3qpi33ja

3qpi33ja1#

有足够的资源,你可以检查创建一个文件夹,并上传一个文件到谷歌驱动器
正在创建文件夹

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

正在上载文件

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

对于 txt 文件,您可能需要引用此stackoverflow post( text/plain ). 有关特定的google应用程序mime类型,请参阅文档。

相关问题