简体   繁体   English

从Android上传Google云端硬盘图片

[英]Google Drive image upload from android

I am trying to upload a image to google drive using API. 我正在尝试使用API​​将图像上传到Google驱动器。 I ve searched so much but i didn't find a way. 我已经搜索了很多东西,但没有找到方法。 I ve got a demo code which uploads a text file and it works. 我有一个演示代码,可以上传文本文件,并且可以正常工作。 i need to change it to upload image. 我需要更改它以上传图像。 This is the code... 这是代码...

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();
}

How can i change this code to upload a image from file.(by given location of the image in device).? 我如何更改此代码以从文件上传图像。(通过设备中图像的给定位置)? I ve found few tutorials but those are deprecated methods. 我发现很少有教程,但这些教程已被弃用。

First, you need t create a file then write the content in to it. 首先,您无需创建文件,然后将内容写入其中。

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");
                    }
                }
            });
}

Use this code to upload image to google drive... 使用此代码将图片上传到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.

相关问题 将图像从Android应用程序上传到Google驱动器 - Upload image from android application to google drive 我想将图像上传到Google驱动器并在android-eclipse中从Google驱动器检索图像URL吗? - i want to upload image into Google drive and retrieve the image URL from Google drive in android- eclipse ? 如何使用 Google Drive API 从 android 将加密的图像文件上传到 Google Drive - How to upload encrypted image file on google drive from android using Google Drive API 在Android中以编程方式使用Google Drive API将图像上传到Google Drive - Upload image to a google drive using google drive api programatically in android Android无需压缩即可将图像上传到Google驱动器 - Android upload image to google drive without compression 将文件从Android应用上传到Google驱动器 - Upload file from android app to google drive 如何将图像从我的Android应用程序上传到Google驱动器上的特定文件夹 - how to upload an image from my android app to a specific folder on google drive 从Google Drive Android API下载图片? - Download Image from google drive Android API? Android从谷歌驱动器获取图像内存不足错误 - Android Getting image from google drive outofmemoryerror 从Android中的Google Drive获取图像缩略图 - Get image thumbnail from google drive in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM