Google drive API on android - bring an image based on your DriveId

1

Good morning, I'm working with the google API on Android, and I found the following code to read a text file:

private void readFile(DriveId fileDriveId) {

        DriveFile file = fileDriveId.asDriveFile();

        file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null)
                .setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
                    @Override
                    public void onResult(DriveApi.DriveContentsResult result) {
                        if (!result.getStatus().isSuccess()) {
                            Log.e(TAG,"Error al abrir fichero (readFile)");
                            return;
                        }

                        DriveContents contents = result.getDriveContents();

                        BufferedReader reader =
                                new BufferedReader(
                                        new InputStreamReader(contents.getInputStream()));

                        StringBuilder builder = new StringBuilder();
                        try {
                            String line;
                            while ((line = reader.readLine()) != null) {
                                builder.append(line);
                            }
                        } catch (IOException e) {
                            Log.e(TAG,"Error al leer fichero");
                        }

                        contents.discard(mGoogleApiClient);

                        Log.i(TAG, "Fichero leido: " + builder.toString());
                    }
                });
    }

I tried the code and it works, but what I need to do, is that instead of reading a text file, the code will return the bitmap of the image I want to recover

How could I do that?

    
asked by L. Ronquillo 14.06.2017 в 17:50
source

1 answer

1

I found the answer, the method is as follows:

private void readFile(DriveId fileDriveId) {

        DriveFile file = fileDriveId.asDriveFile();
        file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null)
                .setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
                    @Override
                    public void onResult(DriveApi.DriveContentsResult result) {
                        if (!result.getStatus().isSuccess()) {
                            Log.e(TAG,"Error al abrir fichero (readFile)");
                            return;
                        }

                        DriveContents contents = result.getDriveContents();

                        //BufferedReader reader =new BufferedReader(new InputStreamReader(contents.getInputStream()));
                        Bitmap bmap = null;
                        InputStream is = contents.getInputStream();
                        bmap = BitmapFactory.decodeStream(is);
                        imgPrueba.setImageBitmap(bmap);
                        /*StringBuilder builder = new StringBuilder();
                        try {
                            String line;
                            while ((line = reader.readLine()) != null) {
                                builder.append(line);
                            }
                        } catch (IOException e) {
                            Log.e(TAG,"Error al leer fichero");
                        }*/

                        contents.discard(mGoogleApiClient);

                        //Log.i(TAG, "Fichero leido: " + builder.toString());
                    }
                });
    }

I purposely left the original code so you know what I modified, a greeting!

    
answered by 14.06.2017 / 18:55
source