簡體   English   中英

如何從Google Drive導出和導入文件?

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

我使用意向到我的.db文件導出到谷歌驅動器。 並通過設備上的本地文件夾導入.db文件。

  1. 如何再次將文件從驅動器導入到設備?
  2. “備份” .db文件並導入文件的最佳方法是什么?
  3. 這是可以做到的行動,而不使用“Google雲端硬盤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卡上創建文件的功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM