如何清除google drive API的文件大小隐藏应用数据?

时间:2017-04-06 06:51:20

标签: android google-drive-api google-drive-android-api

我正在使用Google drive API备份SQLite上的AppFolder文件。它的工作很好。问题是每次备份时内存大小都会增加。初始备份文件大小为1KB,但是当我单击备份按钮时,每次我的应用文件的AppFolder文件大小都增加了。 如何在AppFolder上检查文件是否存在?                                                                                       如果存在,那么如何清除我的应用AppFolder文件的内存,那么只需在AppFolder上备份我的文件。请给我一些解决方案。我被困2天来解决这个问题。但我不能......

在上传之前,在哪里添加搜索查询以搜索文件并删除该文件? 我添加了代码。 添加代码: Bakup Button Listener:

backupButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Drive.DriveApi.newDriveContents(mGoogleApiClient)
                        .setResultCallback(driveContentsCallback);
            }
        });

然后,在其回调中,

final private ResultCallback<DriveApi.DriveContentsResult> driveContentsCallback = new
            ResultCallback<DriveApi.DriveContentsResult>() {
                @Override
                public void onResult(DriveApi.DriveContentsResult result) {
                    if (!result.getStatus().isSuccess()) {
                        return;
                    }

                    final DriveContents driveContents = result.getDriveContents();
                    new Thread() {
                        @Override
                        public void run() {
                            OutputStream outputStream = driveContents.getOutputStream();
                            FileInputStream inputStream = null;
                            File file = changeSQLiteToJSON();
                            try {
                                try {
                                    inputStream = new FileInputStream(file);
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                }
                                byte[] buf = new byte[1024];
                                int bytesRead;
                                try {
                                    if (inputStream != null) {
                                        while ((bytesRead = inputStream.read(buf)) > 0) {
                                            outputStream.write(buf, 0, bytesRead);
                                        }
                                    }
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            } catch (NullPointerException ex) {
                                ex.printStackTrace();
                            } finally {
                                file.delete();
                            }

                            MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                                    .setTitle(JSON_NAME)
                                    .setMimeType("text/plain")
                                    .setStarred(true).build();

                            Drive.DriveApi.getAppFolder(mGoogleApiClient)
                                    .createFile(mGoogleApiClient, changeSet, driveContents)
                                    .setResultCallback(fileCallback);
                        }
                    }.start();
                }

            };

FinalCallBack:

final private ResultCallback<DriveFolder.DriveFileResult> fileCallback = new
            ResultCallback<DriveFolder.DriveFileResult>() {
                @Override
                public void onResult(DriveFolder.DriveFileResult result) {
                    if (!result.getStatus().isSuccess()) {
                        return;
                    }
                    Toast.makeText(getApplicationContext(), "Created a file with content: " + result.getDriveFile().getDriveId(), Toast.LENGTH_SHORT).show();
                }
            };

1 个答案:

答案 0 :(得分:0)

此代码适用于我::

设置错误

 backupButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isUploading)
                    return;
                try {
                    isUploading = true;
                    Drive.DriveApi.requestSync(mGoogleApiClient)
                            .setResultCallback(fileCheckCallBack);
                } catch (IllegalStateException ex) {

                }
            }
        });

BackupButton侦听器

 private ResultCallback<Status> fileCheckCallBack = new ResultCallback<Status>() {
        @Override
        public void onResult(@NonNull Status status) {
            if (!status.isSuccess()) {
            }
            Query query = new Query.Builder()
                    .addFilter(Filters.contains(SearchableField.TITLE, JSON_NAME))
                    .build();
            Drive.DriveApi.query(mGoogleApiClient, query)
                    .setResultCallback(deleteMetaDataCallback);
        }
    };

fileCheckCallBack用于从appfolder查询元数据标题

final private ResultCallback<DriveApi.MetadataBufferResult> deleteMetaDataCallback =
            new ResultCallback<DriveApi.MetadataBufferResult>() {
                @Override
                public void onResult(DriveApi.MetadataBufferResult result) {
                    if (!result.getStatus().isSuccess()) {
                        Toast.makeText(getBaseContext(), "Problem while retrieving results", Toast.LENGTH_LONG).show();
                        isUploading = false;
                        return;
                    }

                    MetadataBuffer mdb = null;
                    mdb = result.getMetadataBuffer();
                    for (Metadata md : mdb) {
                        DriveId driveId = md.getDriveId();
                        String name = md.getTitle();
                        if (name.equals(JSON_NAME)) {
                            driveId.asDriveResource().delete(mGoogleApiClient).setResultCallback(deleteSuccessCallBack);
                            return;
                        }
                    }

                    if (mdb != null) mdb.close();
                    Drive.DriveApi.newDriveContents(mGoogleApiClient)
                            .setResultCallback(driveContentsCallback);

                }
            };

deleteMetaDataCallback(如果文件存在于app文件夹中则删除该文件,否则创建新文件)

final ResultCallback<Status> deleteSuccessCallBack =
            new ResultCallback<Status>() {
                @Override
                public void onResult(@NonNull Status status) {
                    if (!status.isSuccess()) {
                        isUploading = false;
                        return;
                    }
                    Drive.DriveApi.newDriveContents(mGoogleApiClient)
                            .setResultCallback(driveContentsCallback);
                }

            };

成功完全从app文件夹中删除:deletesuccessCallBack

final private ResultCallback<DriveApi.DriveContentsResult> driveContentsCallback = new
            ResultCallback<DriveApi.DriveContentsResult>() {
                @Override
                public void onResult(DriveApi.DriveContentsResult result) {
                    if (!result.getStatus().isSuccess()) {
                        isUploading = false;
                        return;
                    }

                    final DriveContents driveContents = result.getDriveContents();
                    new Thread() {
                        @Override
                        public void run() {
                            OutputStream outputStream = driveContents.getOutputStream();
                            FileInputStream inputStream = null;
                            File file = changeSQLiteToJSON();
                            try {
                                try {
                                    inputStream = new FileInputStream(file);
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                }
                                byte[] buf = new byte[1024];
                                int bytesRead;
                                try {
                                    if (inputStream != null) {
                                        while ((bytesRead = inputStream.read(buf)) > 0) {
                                            outputStream.write(buf, 0, bytesRead);
                                        }
                                    }
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            } catch (NullPointerException ex) {
                                ex.printStackTrace();
                            } finally {
                                file.delete();
                            }
                            MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                                    .setTitle(JSON_NAME)
                                    .setMimeType("text/plain")
                                    .setStarred(true).build();

                            Drive.DriveApi.getAppFolder(mGoogleApiClient)
                                    .createFile(mGoogleApiClient, changeSet, driveContents)
                                    .setResultCallback(fileCallback);
                        }
                    }.start();
                }

            };

然后,在其driveContentsCallback

final private ResultCallback<DriveFolder.DriveFileResult> fileCallback = new
            ResultCallback<DriveFolder.DriveFileResult>() {
                @Override
                public void onResult(DriveFolder.DriveFileResult result) {
                    isUploading = false;
                    if (!result.getStatus().isSuccess()) {
                        return;
                    }
                    Toast.makeText(getApplicationContext(), "Created a file with content: " + result.getDriveFile().getDriveId(), Toast.LENGTH_SHORT).show();
                }
            };

fileCallBack

2100/2/2