簡體   English   中英

從Android上傳Google雲端硬盤圖片

[英]Google Drive image upload from android

我正在嘗試使用API​​將圖像上傳到Google驅動器。 我已經搜索了很多東西,但沒有找到方法。 我有一個演示代碼,可以上傳文本文件,並且可以正常工作。 我需要更改它以上傳圖像。 這是代碼...

public void CreateFileOnGoogleDrive(DriveContentsResult result){


    final DriveContents driveContents = result.getDriveContents();

    // Perform I/O off the UI thread.
    new Thread() {
        @Override
        public void run() {
            // write content to DriveContents
            OutputStream outputStream = driveContents.getOutputStream();
            Writer writer = new OutputStreamWriter(outputStream);
            try {
                writer.write("Hello abhay!");
                writer.close();
            } catch (IOException e) {
                Log.e(TAG, e.getMessage());
            }

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

            // create a file in root folder
            Drive.DriveApi.getRootFolder(mGoogleApiClient)
                    .createFile(mGoogleApiClient, changeSet, driveContents)
                    .setResultCallback(fileCallback);
        }
    }.start();
}

我如何更改此代碼以從文件上傳圖像。(通過設備中圖像的給定位置)? 我發現很少有教程,但這些教程已被棄用。

首先,您無需創建文件,然后將內容寫入其中。

private void saveFiletoDrive(final File file, final String mime) {
    Drive.DriveApi.newDriveContents(mDriveClient).setResultCallback(
            new ResultCallback<DriveContentsResult>() {
                @Override
                public void onResult(DriveContentsResult result) {
                    if (!result.getStatus().isSuccess()) {
                        Log.i(TAG, "Failed to create new contents.");
                        return;
                    }
                     Log.i(TAG, "Connection successful, creating new contents...");
                    OutputStream outputStream = result.getDriveContents()
                            .getOutputStream();
                    FileInputStream fis;
                    try {
                        fis = new FileInputStream(file.getPath());
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        byte[] buf = new byte[1024];
                        int n;
                        while (-1 != (n = fis.read(buf)))
                            baos.write(buf, 0, n);
                        byte[] photoBytes = baos.toByteArray();
                        outputStream.write(photoBytes);

                        outputStream.close();
                        outputStream = null;
                        fis.close();
                        fis = null;

                    } catch (FileNotFoundException e) {
                        Log.w(TAG, "FileNotFoundException: " + e.getMessage());
                    } catch (IOException e1) {
                        Log.w(TAG, "Unable to write file contents." + e1.getMessage());
                    }

                    String title = file.getName();
                    MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                            .setMimeType(mime).setTitle(title).build();

                    if (mime.equals(MIME_PHOTO)) {
                        if (VERBOSE)
                            Log.i(TAG, "Creating new photo on Drive (" + title
                                    + ")");
                        Drive.DriveApi.getFolder(mDriveClient,
                                mPicFolderDriveId).createFile(mDriveClient,
                                metadataChangeSet,
                                result.getDriveContents());
                    } else if (mime.equals(MIME_VIDEO)) {
                        Log.i(TAG, "Creating new video on Drive (" + title
                                + ")");
                        Drive.DriveApi.getFolder(mDriveClient,
                                mVidFolderDriveId).createFile(mDriveClient,
                                metadataChangeSet,
                                result.getDriveContents());
                    }

                    if (file.delete()) {
                        if (VERBOSE)
                            Log.d(TAG, "Deleted " + file.getName() + " from sdcard");
                    } else {
                        Log.w(TAG, "Failed to delete " + file.getName() + " from sdcard");
                    }
                }
            });
}

使用此代碼將圖片上傳到Google驅動器...

new Thread() {
            @Override
            public void run() {
                // write content to DriveContents
                OutputStream outputStream = driveContents.getOutputStream();
                // Write the bitmap data from it.
                MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                        .setMimeType("image/jpeg").setTitle(title)
                        .build();
                Bitmap image = BitmapFactory.decodeFile(location));
                ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
                image.compress(Bitmap.CompressFormat.JPEG, 80, bitmapStream);
                try {
                    outputStream.write(bitmapStream.toByteArray());
                } catch (IOException e1) {
                    Log.i("E", "Unable to write file contents.");
                }
                image.recycle();
                outputStream = null;
                String title = "noisy";

                Log.i("E", "Creating new pic on Drive (" + title + ")");
                Drive.DriveApi.getFolder(mGoogleApiClient,driveId)
                        .createFile(mGoogleApiClient, metadataChangeSet, driveContents)
                        .setResultCallback(fileCallback);
            }
        }.start();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM