android – 如何使用setResultCallback方法?

android – 如何使用setResultCallback方法?,第1张

概述import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.OutputStream;import android.app.Activity;import android.content.Intent;import android.content.IntentSender;import
import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.OutputStream;import androID.app.Activity;import androID.content.Intent;import androID.content.IntentSender;import androID.content.IntentSender.SendIntentException;import androID.graphics.Bitmap;import androID.os.Bundle;import androID.provIDer.MediaStore;import androID.util.Log;import com.Google.androID.gms.common.ConnectionResult;import com.Google.androID.gms.common.GooglePlayServicesUtil;import com.Google.androID.gms.common.API.Googleapiclient;import com.Google.androID.gms.common.API.Googleapiclient.ConnectionCallbacks;import com.Google.androID.gms.common.API.Googleapiclient.OnConnectionFailedListener;import com.Google.androID.gms.drive.Drive;import com.Google.androID.gms.drive.DriveAPI.ContentsResult;import com.Google.androID.gms.drive.MetadataChangeSet;public class Phototodrive extends Activity implements ConnectionCallbacks,OnConnectionFailedListener{    private static final String TAG = "androID-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.newContents(mGoogleapiclient).setResultCallback(new ResultCallback<ContentsResult>() {             public voID onResult(ContentsResult 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.getContents().getoutputStream();                // Write the bitmap data from it.                ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();                image.compress(Bitmap.CompressFormat.PNG,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.png").build();                // Create an intent for the file chooser,and start it.                IntentSender intentSender = Drive.DriveAPI                        .newCreatefileActivityBuilder()                         .setinitialMetadata(MetadataChangeSet)                        .setinitialContents(result.getContents())                        .build(mGoogleapiclient);                try {                    startIntentSenderForResult(                            intentSender,REQUEST_CODE_CREATOR,null,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);                }                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.            GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(),this,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 ondisconnected() {        Log.i(TAG,"Googleapiclient connection suspended");    }   }

我正在尝试运行androID Google drive API quickstart应用程序.
我得到一个错误
– 对于PendingResult类型,方法setResultCallback(new ResultCallback(){})未定义
– 无法将ResultCallback解析为某种类型
我应该更改什么,或者为什么我收到此错误?

解决方法 由于’ondisconnect()’的存在,代码看起来过时(Google Play Services lib ver 14).检查您是否拥有版本15(AndroID SDK Manager),然后尝试 quickstart,demos或 this code.您的设备应该在Google Play Services 4.2上… 总结

以上是内存溢出为你收集整理的android – 如何使用setResultCallback方法?全部内容,希望文章能够帮你解决android – 如何使用setResultCallback方法?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1125186.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存