如何从谷歌驱动器导出和导入文件?

时间:2015-02-02 22:40:01

标签: android android-external-storage

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

  1. 如何再次从驱动器将文件导入我的设备?
  2. "备份"最好的方式是什么? .db文件并导入文件?
  3. 可以在不使用" google drive api"
  4. 的情况下执行该操作

    请帮帮我!

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

1 个答案:

答案 0 :(得分:-1)

由于Android 4.4(KitKat),在外部SD卡上创建文件的功能已被删除。