上传sqlite db文件到谷歌驱动器后,它显示文件大小为0字节

时间:2017-04-14 04:50:40

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

朋友们,我已经将我的sqlite db文件从我的应用程序上传到谷歌驱动器,但文件大小显示0字节为什么?如何解决它以及如何上传实际原始文件(大小为108 kb)到谷歌驱动器。有没有其他解决方案,请任何人帮助我这是我的代码

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi;
import com.google.android.gms.drive.DriveContents;
import com.google.android.gms.drive.DriveFolder;
import com.google.android.gms.drive.Metadata;
import com.google.android.gms.drive.MetadataChangeSet;
import com.google.android.gms.drive.query.Filters;
import com.google.android.gms.drive.query.Query;
import com.google.android.gms.drive.query.SearchableField;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class MainActivitySample extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private Button button_create_file;
private Button button_upload_to_google_drive;
private GoogleApiClient mGoogleApiClient;
private static final String TAG = "<< DRIVE >>";
protected static final int REQUEST_CODE_RESOLUTION = 1337;
private String FOLDER_NAME = "New Folder";
com.google.android.gms.common.api.GoogleApiClient GAC;

Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_sample);


    button_create_file = (Button) findViewById(R.id.button_create_file);
    button_upload_to_google_drive = (Button) findViewById(R.id.button_upload_to_google_drive);


    button_create_file.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //WRITE A SIMPLE TEXT FILE IN SDCARD. BE CAREFUL TO GRANT PERMISSION IN ANDROID 6+
            //writeToFile("tehfile", " I nenu");

        }
    });


    button_upload_to_google_drive.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mGoogleApiClient != null) {
                upload_to_drive();
            } else {
                Log.e(TAG, "Could not fucking connect to google drive manager");
            }
        }
    });


}



private void upload_to_drive() {

    //async check if folder exists... if not, create it. continue after with create_file_in_folder(driveId);
    check_folder_exists();
}

private void check_folder_exists() {
    Query query =
            new Query.Builder().addFilter(Filters.and(Filters.eq(SearchableField.TITLE, FOLDER_NAME), Filters.eq(SearchableField.TRASHED, false)))
                    .build();
    Drive.DriveApi.query(mGoogleApiClient, query).setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
        @Override
        public void onResult(DriveApi.MetadataBufferResult result) {
            if (!result.getStatus().isSuccess()) {
                Log.e(TAG, "Cannot create folder in the root.");
            } else {
                boolean isFound = false;
                for (Metadata m : result.getMetadataBuffer()) {
                    if (m.getTitle().equals(FOLDER_NAME)) {
                        Log.e(TAG, "Folder exists");
                        isFound = true;
                        DriveId driveId = m.getDriveId();
                        create_file_in_folder(driveId);
                        break;
                    }
                }
                if (!isFound) {
                    Log.i(TAG, "Folder not found; creating it.");
                    MetadataChangeSet changeSet = new MetadataChangeSet.Builder().setTitle(FOLDER_NAME).build();
                    Drive.DriveApi.getRootFolder(mGoogleApiClient)
                            .createFolder(mGoogleApiClient, changeSet)
                            .setResultCallback(new ResultCallback<DriveFolder.DriveFolderResult>() {
                                @Override
                                public void onResult(DriveFolder.DriveFolderResult result) {
                                    if (!result.getStatus().isSuccess()) {
                                        Log.e(TAG, "U AR A MORON! Error while trying to create the folder");
                                    } else {
                                        Log.i(TAG, "Created a folder");
                                        DriveId driveId = result.getDriveFolder().getDriveId();
                                        create_file_in_folder(driveId);
                                    }
                                }
                            });
                }
            }
        }
    });
}

private void create_file_in_folder(final DriveId driveId) {

    Drive.DriveApi.newDriveContents(mGoogleApiClient).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
        @Override
        public void onResult(@NonNull DriveApi.DriveContentsResult driveContentsResult) {
            if (!driveContentsResult.getStatus().isSuccess()) {
                Log.e(TAG, "U AR A MORON! Error while trying to create new file contents");
                return;
            }

            OutputStream outputStream = driveContentsResult.getDriveContents().getOutputStream();

            //------ THIS IS AN EXAMPLE FOR FILE --------
            Toast.makeText(MainActivitySample.this, "Uploading to drive...", Toast.LENGTH_LONG).show();
            final File theFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Hello/IKONNECTRETAIL.db"); //>>>>>> WHAT FILE ?
            try {
                FileInputStream fileInputStream = new FileInputStream(theFile);
                byte[] buffer = new byte[1024];
                //long size = theFile.length();
                int bytesRead;
                while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
            } catch (IOException e1) {
                Log.i(TAG, "U AR A MORON! Unable to write file contents.");
            }
            String mimeType = MimeTypeMap.getSingleton().getExtensionFromMimeType("db");
            MetadataChangeSet changeSet = new MetadataChangeSet.Builder().setTitle(theFile.getName()).setMimeType(mimeType).setStarred(false).build();
            DriveFolder folder = driveId.asDriveFolder();
            folder.createFile(mGoogleApiClient, changeSet, driveContentsResult.getDriveContents())
                    .setResultCallback(new ResultCallback<DriveFolder.DriveFileResult>() {
                        @Override
                        public void onResult(@NonNull DriveFolder.DriveFileResult driveFileResult) {
                            if (!driveFileResult.getStatus().isSuccess()) {
                                Log.e(TAG, "U AR A MORON!  Error while trying to create the file");
                                return;
                            }
                            Log.v(TAG, "Created a file: " + driveFileResult.getDriveFile().getDriveId());
                        }
                    });
        }
    });
}

@Override
protected void onResume() {
    super.onResume();
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addScope(Drive.SCOPE_APPFOLDER) // required for App Folder sample
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }
    mGoogleApiClient.connect();
}

public void writeToFile(String fileName, String body) {
    FileOutputStream fos = null;
    try {
        final File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/xtests/");
        if (!dir.exists()) {
            if (!dir.mkdirs()) {
                Log.e("ALERT", "U AR A MORON!  could not create the directories. CHECK THE FUCKING PERMISSIONS SON!");
            }
        }
        final File myFile = new File(dir, fileName + "_" + String.valueOf(System.currentTimeMillis()) + ".txt");
        if (!myFile.exists()) {
            myFile.createNewFile();
        }

        fos = new FileOutputStream(myFile);
        fos.write(body.getBytes());
        fos.close();
        Toast.makeText(MainActivitySample.this, "File created ok! Let me give you a fucking congratulations!", Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    Log.v(TAG, "+++++++++++++++++++ onConnected +++++++++++++++++++");
}

@Override
public void onConnectionSuspended(int i) {
    Log.e(TAG, "onConnectionSuspended [" + String.valueOf(i) + "]");
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult result) {
    Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
    if (!result.hasResolution()) {
        // show the localized error dialog.
        GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
        return;
    }
    try {
        result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
    } catch (IntentSender.SendIntentException e) {
        Log.e(TAG, "U AR A MORON! Exception while starting resolution activity", e);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == RESULT_OK) {
        mGoogleApiClient.connect();
    }
}

@Override
protected void onPause() {
    if (mGoogleApiClient != null) {
        mGoogleApiClient.disconnect();
    }
    super.onPause();
}
}

这是驱动器图片 enter image description here

请帮我谢谢你

1 个答案:

答案 0 :(得分:0)

我的代码中没有找到任何 .commit()。 文件操作后,您必须调用

contentsResult.getDriveContents().commit(mGoogleApiClient, null)

.discard(GoogleApiClient mGoogleApiClient)

the link

下的更多信息

创建文件后,只需执行

DriveFile file = driveFileResult.getDriveFile();
file.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
    @Override
    public void onResult(@NonNull DriveApi.DriveContentsResult driveContentsResult) {
        OutputStream outputStream = contentsResult.getDriveContents().getOutputStream();
        //your code here to write into output stream
        outputStream.close();
        contentsResult.getDriveContents().commit(mGoogleApiClient, null);
    }