简体   繁体   English

如何从Google Drive导出和导入文件?

[英]How to export and import file from google drive?

I'm using Intent to export my .db file to the google drive. 我使用意向到我的.db文件导出到谷歌驱动器。 and import the .db file through the local folder on the device. 并通过设备上的本地文件夹导入.db文件。

  1. how can i import the file to my device from drive again? 如何再次将文件从驱动器导入到设备?
  2. what is the best way to "backup" the .db file and import the file? “备份” .db文件并导入文件的最佳方法是什么?
  3. it's possible to do that action without using "google drive api"? 这是可以做到的行动,而不使用“Google云端硬盘API”?

Help me please ! 请帮帮我 !

 public class BackUpDb {

        Context context;

        public BackUpDb(Context activity) {
            this.context = activity;
            }

        public void exportDb(){
            File direct = new File(Environment
                    .getExternalStorageDirectory()
                    + "/folderToBeCreated");
            if (!direct.exists()) {
                if (direct.mkdir()) {
                    // directory is created;
                }
            }
            try {
                exportDB();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


    public void importDb(){
         importDB();
    }



    // importing database
    @SuppressLint("SdCardPath")
    private void importDB() {
        try {
    //      File file = getBaseContext().getFileStreamPath(Environment.getExternalStorageDirectory()
    //              + "/MyDatabase");
    //      if(file.exists()){

                FileInputStream fis = new FileInputStream(Environment.getExternalStorageDirectory()
                        + "/folderToBeCreated/MyDatabase");       

                String outFileName = "/data/data/com.example.application/databases/"+DbHandler.DB_NAME;

                OutputStream output = new FileOutputStream(outFileName);
                byte[] buffer = new byte[1024];
                int length;
                while ((length = fis.read(buffer)) > 0) {
                    output.write(buffer, 0, length);
                }
                // Close the streams
                output.flush();
                output.close();
                fis.close();
                Toast.makeText(context, "Ok :)",Toast.LENGTH_LONG).show();




        } catch (Exception e) {
            Toast.makeText(context, "No list found",
                    Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }


    @SuppressLint("SdCardPath")
    private void exportDB() throws IOException {
        // Open your local db as the input stream
        try {
            String inFileName = "/data/data/com.example.application/databases/"+DbHandler.DB_NAME;
            File dbFile = new File(inFileName);
            FileInputStream fis = new FileInputStream(dbFile);

          String outFileName = Environment.getExternalStorageDirectory()+ "/folderToBeCreated/MyDatabase";


            // Open the empty db as the output stream
            OutputStream output = new FileOutputStream(outFileName);
            // transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[1024];
            int length;
            while ((length = fis.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            // Close the streams
            output.flush();
            output.close();
            fis.close();







        } catch (Exception e) {
            Toast.makeText(context, e.toString(), Toast.LENGTH_LONG)
                    .show();
        }
        Toast.makeText(context,
                "save to :\n/folderToBeCreated",Toast.LENGTH_LONG).show();


    }


    public void sendDb(String mailAddres){

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("application/octet-stream");
        intent.putExtra(Intent.EXTRA_EMAIL, new String[] {mailAddres});
        intent.putExtra(Intent.EXTRA_SUBJECT, "MyDatabase");

        File root = Environment.getExternalStorageDirectory();
        File file = new File(root, "/folderToBeCreated/MyDatabase");
        if (!file.exists() || !file.canRead()) {
            Toast.makeText(context, "No sd-card", Toast.LENGTH_SHORT).show();
            return;
        }
        Uri uri = Uri.parse("file://" + file.getAbsolutePath());
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        context.startActivity(Intent.createChooser(intent, "BACKUP"));
    }



    }

从Android 4.4(KitKat)开始,已删除了在外部SD卡上创建文件的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM