Google Drive Android api - Downloading db file from drive

后端 未结 2 471
感动是毒
感动是毒 2021-01-07 10:12

I am using the below code for downloading an already uploaded sqlite db file from google drive to the data/data/packagename/databases folder, but when the method completes,

相关标签:
2条回答
  • 2021-01-07 10:16

    I was able to finally fix my own issue, by replacing the file.openContents with file.open and setting a result call back with DriveContents result back rather than ContentsResult.

    Also, used the DriveContents to set an InputStream on the same (initially opened in Mode_Read_Only in open method) and then writing it out into a physical ".db" file on the data base path location.

    Now, the database doesn't get corrupted and restores data successfully.

    0 讨论(0)
  • 2021-01-07 10:31

    This solution is not working for me. The sqlite files I got from google drive is more longer than I saved in google drive. That, probably, happen because you are using a Buffereader class and the method readline that reads data in char. In my project and experience this solutions does not work, I got a corrupted sqlite db. So... I save sqlite db in google drive in byte so **I read from google drive the same sqlite db in byte, using BufferedInputStream. This works perfectly for me:

    DriveContents contents = result.getDriveContents();
                    BufferedInputStream bis = new BufferedInputStream(contents.getInputStream());
                    byte[] buffer = new byte[1024];
                    int bytesread = 0;
                    FileOutputStream outStream;
    
                    /*if(currentDB != null)
                        currentDB.delete();*/
    
                    try
                    {
                        outStream = new FileOutputStream(currentDB);
                        while( (bytesread = bis.read(buffer)) != -1)
                        {
                            outStream.write(buffer,0,bytesread);
                        }
    
                        outStream.flush();
                        bis.close();
                        outStream.close();
                    }
                    catch (FileNotFoundException e)
                    {
                        Log.i(TAG,e.getMessage());
                    }
                    catch (IOException e)
                    {
                        Log.i(TAG,e.getMessage());
                    }
                    finally {
                        Toast.makeText(getActivity().getBaseContext(),"Data from Google Drive restored successfully." , Toast.LENGTH_LONG).show();
                    }
                    contents.discard(mGoogleApiClient);
    
    0 讨论(0)
提交回复
热议问题