0

I'm trying to solve this problem for a long time. This part of code download image to GDrive.

public void UploadFileOnGoogleDrive(DriveContentsResult result){
    final DriveContents driveContents=result.getDriveContents();

    new Thread(){
        @Override
        public void run(){
            OutputStream outputStream = driveContents.getOutputStream();
            // Write the bitmap data from it.
            String photo = "IMG23.jpg";

            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),photo);
            Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
            ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();

            int numOfbytes = myBitmap.getByteCount();
            ByteBuffer buffer = ByteBuffer.allocate(numOfbytes);
            myBitmap.copyPixelsToBuffer(buffer);

            //imageInByte = buffer.array();

            myBitmap.compress(Bitmap.CompressFormat.JPEG, 80, bitmapStream);

            try {
                outputStream.write(bitmapStream.toByteArray());
                //outputStream.write(bitmapStream.toByteArray());
            } catch (IOException e1) {
                Log.i(TAG, "Unable to write file contents.");
            }
            MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                    .setMimeType("image/jpeg")
                    .setTitle(photo)
                    .setIndexableText(photo)
                    .build();

            Log.d("Description",metadataChangeSet.getIndexableText());



            Drive.DriveApi.getRootFolder(mGoogleApiClient)
                    .createFile(mGoogleApiClient, metadataChangeSet, driveContents).
                    setResultCallback(fileCallback);

           //Drive.DriveApi.fetchDriveId(mGoogleApiClient,EXISTING_FILE_ID).setResultCallback(idfileCallback);


        }
    }.start();
}

Here I try to get file ID

final private ResultCallback<DriveFolder.DriveFileResult> fileCallback = new
        ResultCallback<DriveFolder.DriveFileResult>() {

            @Override
            public void onResult(DriveFolder.DriveFileResult result) {
                if (result.getStatus().isSuccess()) {
                    Toast.makeText(getApplicationContext(), "file created: "+""+
                            result.getDriveFile().getDriveId(), Toast.LENGTH_LONG).show();
                    //DriveFile file = Drive.DriveApi.getFile(mGoogleApiClient, result.getDriveFile().getDriveId());
                    //file.getMetadata(mGoogleApiClient)
                      //      .setResultCallback(metadataRetrievedCallback);

                    //String id=file.getDriveId().encodeToString();
                    Log.d("Fileidis",result.getDriveFile().getDriveId().encodeToString());

                    //final DriveId fileid = result.getDriveFile().getDriveId();

                    //DriveFile driveFile=Drive.DriveApi.getFile(mGoogleApiClient,fileid);
                }
                return;
            }
        };

But we find not fully ID in logs

07-06 15:15:50.939 19899-19899/valery.pankov.gdriveapp D/Fileidis: DriveId:CAESABj4XSCG9Oqbt1EoAA==

I thought set idResultCallback on UploadFileOnGoogleDrive but it was not successfull. Thanks in advance

  • 2
    I'm not sure if that's what you mean but try to look here: http://stackoverflow.com/questions/22874657/unpredictable-result-of-driveid-getresourceid-in-google-drive-android-api/31553269#31553269 – Anatoli Jul 07 '16 at 05:22
  • @Anatoli , Thanks a lot! It was very helpfull :) – Valery Pankov Jul 07 '16 at 11:16

0 Answers0