简体   繁体   English

Android无需压缩即可将图像上传到Google驱动器

[英]Android upload image to google drive without compression

I have followed the google drive api documentation, and based my code on the demo that captures a picture from device's camera, and uploads it to google drive. 我遵循了Google Drive API文档,并基于演示获取代码,该演示从设备的摄像头捕获图片并将其上传到Google Drive。

Google Demo - Takes photos and stores them in Drive Google演示-拍摄照片并将其存储在云端硬盘中

However, the uploaded image seems to have small resolution , and it doesn't upload the full resolution image. 但是,上传的图片似乎分辨率较低 ,并且没有上传完整分辨率的图片。

How can I upload the full resolution image that I took with my phone's camera? 如何上传用手机相机拍摄的全分辨率图像?

my code: 我的代码:

public class UploadImgToDrive extends Activity implements ConnectionCallbacks,
    OnConnectionFailedListener {

private static final String TAG = "drive-quickstart";
private static final int REQUEST_CODE_CAPTURE_IMAGE = 1;
private static final int REQUEST_CODE_CREATOR = 2;
private static final int REQUEST_CODE_RESOLUTION = 3;

private GoogleApiClient mGoogleApiClient;
private Bitmap mBitmapToSave;


/**
 * Create a new file and save it to Drive.
 */
private void saveFileToDrive() {
    // Start by creating a new contents, and setting a callback.
    Log.i(TAG, "Creating new contents.");
    final Bitmap image = mBitmapToSave;
    Drive.DriveApi.newDriveContents(mGoogleApiClient)
            .setResultCallback(new ResultCallback<DriveContentsResult>() {

                @Override
                public void onResult(DriveContentsResult result) {
                    // If the operation was not successful, we cannot do anything
                    // and must
                    // fail.
                    if (!result.getStatus().isSuccess()) {
                        Log.i(TAG, "Failed to create new contents.");
                        return;
                    }
                    // Otherwise, we can write our data to the new contents.
                    Log.i(TAG, "New contents created.");
                    // Get an output stream for the contents.
                    OutputStream outputStream = result.getDriveContents().getOutputStream();
                    // Write the bitmap data from it.
                    ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
                    image.compress(Bitmap.CompressFormat.JPEG, 100, bitmapStream);

                    try {
                        outputStream.write(bitmapStream.toByteArray());
                    } catch (IOException e1) {
                        Log.i(TAG, "Unable to write file contents.");
                    }
                    // Create the initial metadata - MIME type and title.
                    // Note that the user will be able to change the title later.
                    MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                            .setMimeType("image/jpeg").setTitle("Android Photo.jpeg").build();
                    // Create an intent for the file chooser, and start it.
                    IntentSender intentSender = Drive.DriveApi
                            .newCreateFileActivityBuilder()
                            .setInitialMetadata(metadataChangeSet)
                            .setInitialDriveContents(result.getDriveContents())
                            .build(mGoogleApiClient);
                    try {
                        startIntentSenderForResult(
                                intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0);
                    } catch (SendIntentException e) {
                        Log.i(TAG, "Failed to launch file chooser.");
                    }
                }
            });
}

@Override
protected void onResume() {
    super.onResume();
    if (mGoogleApiClient == null) {
        // Create the API client and bind it to an instance variable.
        // We use this instance as the callback for connection and connection
        // failures.
        // Since no account name is passed, the user is prompted to choose.
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }
    // Connect the client. Once connected, the camera is launched.
    mGoogleApiClient.connect();
}

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

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    switch (requestCode) {
        case REQUEST_CODE_CAPTURE_IMAGE:
            // Called after a photo has been taken.
            if (resultCode == Activity.RESULT_OK) {
                // Store the image data as a bitmap for writing later.
                mBitmapToSave = (Bitmap) data.getExtras().get("data");
            }
            break;
        case REQUEST_CODE_CREATOR:
            // Called after a file is saved to Drive.
            if (resultCode == RESULT_OK) {
                Log.i(TAG, "Image successfully saved.");
                mBitmapToSave = null;
                // Just start the camera again for another photo.
                //startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
                        //REQUEST_CODE_CAPTURE_IMAGE);
                finish();
            }
            break;
    }
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    // Called whenever the API client fails to connect.
    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;
    }
    // The failure has a resolution. Resolve it.
    // Called typically when the app is not yet authorized, and an
    // authorization
    // dialog is displayed to the user.
    try {
        result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
    } catch (SendIntentException e) {
        Log.e(TAG, "Exception while starting resolution activity", e);
    }
}

@Override
public void onConnected(Bundle connectionHint) {
    Log.i(TAG, "API client connected.");
    if (mBitmapToSave == null) {
        // This activity has no UI of its own. Just start the camera.
        startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
                REQUEST_CODE_CAPTURE_IMAGE);
        return;
    }
    saveFileToDrive();
}

@Override
public void onConnectionSuspended(int cause) {
    Log.i(TAG, "GoogleApiClient connection suspended");
}


}

I read A LOT of tutorials and answered questions here, but most are outdated and don't seem to work. 我在这里阅读了很多教程并回答了问题,但是大多数教程已经过时了,似乎没有用。

Also, later on, I will need to upload several images . 另外,稍后,我将需要上传几个图像 How can I do that? 我怎样才能做到这一点?

Thanks in advance. 提前致谢。

However, the uploaded image seems to have small resolution, and it doesn't upload the full resolution image. 但是,上传的图片似乎分辨率较低,并且没有上传完整分辨率的图片。

This ticket is similar on why you're getting a low resolution result from calling the Camera action in your app. 此票证与您为什么通过在应用程序中调用“相机”操作得到低分辨率结果类似。 They were able to resolve it by calling the URI path and casting it as a BitMap to get the original image. 他们能够通过调用URI路径并将其强制转换为BitMap来获取原始图像来解决该问题。

Also, later on, I will need to upload several images. 另外,稍后,我将需要上传几个图像。 How can I do that? 我怎样才能做到这一点?

An approach would be something similar to the Android quickstart , I think you can specify a folder to upload in it all files inside it are uploaded.. 一种方法将类似于Android quickstart ,我认为您可以指定一个文件夹来上载其中的所有文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将照片上传到驱动器而不压缩 - Upload photo to drive without compression 在Android中以编程方式使用Google Drive API将图像上传到Google Drive - Upload image to a google drive using google drive api programatically in android 从Android上传Google云端硬盘图片 - Google Drive image upload from android 将图像从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云端硬盘 - Android resumable upload to Google Drive 如何在不使用压缩的情况下使用Httppost在android中将图像完整质量上传到服务器 - How to upload image to server in full quality in android using Httppost without compression Android图像压缩无需更改 - Android Image Compression Without Res Change Android Google Drive SDK上传文件? - Android Google Drive SDK upload files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM