Android 如何从google drive导出和导入文件?

Android 如何从google drive导出和导入文件?,android,android-external-storage,Android,Android External Storage,我正在使用Intent将.db文件导出到google驱动器。并通过设备上的本地文件夹导入.db文件 如何再次从驱动器将文件导入设备 “备份”该.db文件并导入该文件的最佳方法是什么 不使用“谷歌驱动api”就可以执行该操作吗 请帮帮我 public class BackUpDb { Context context; public BackUpDb(Context activity) { this.context = activity;

我正在使用Intent将.db文件导出到google驱动器。并通过设备上的本地文件夹导入.db文件

  • 如何再次从驱动器将文件导入设备
  • “备份”该.db文件并导入该文件的最佳方法是什么
  • 不使用“谷歌驱动api”就可以执行该操作吗
  • 请帮帮我

     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卡上创建文件的功能已经被删除。

    我不知道,有一种方法可以破解它?为什么?好的,我改变了我的问题,你这是什么意思?你有没有可能和这个有联系?先生因为我是一个小小的怀疑者,但我用的是安卓4.3果冻豆!galaxy 4sokay,anna,你想存储到设备,然后上传到云?这些是你们的要求吗?是的。。你能帮助我吗?