Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从谷歌硬盘获取文件_Android_File_Google Drive Api_Google Drive Android Api - Fatal编程技术网

Android 如何从谷歌硬盘获取文件

Android 如何从谷歌硬盘获取文件,android,file,google-drive-api,google-drive-android-api,Android,File,Google Drive Api,Google Drive Android Api,我想使用此代码从驱动器获取文件,但不工作。下面给出了一些代码- public class PinFileActivity extends BaseDemoActivity { private static final int REQUEST_CODE_OPENER = NEXT_AVAILABLE_REQUEST_CODE; private static final String TAG = "PinFileActivity"; privat

我想使用此代码从驱动器获取文件,但不工作。下面给出了一些代码-

public class PinFileActivity extends BaseDemoActivity {

        private static final int REQUEST_CODE_OPENER = NEXT_AVAILABLE_REQUEST_CODE;

        private static final String TAG = "PinFileActivity";

        private DriveId mFileId;

        /**
         * Starts a file opener intent to pick a file.
         */
        @Override
        public void onConnected(Bundle connectionHint) {
            super.onConnected(connectionHint);
            if (mFileId == null) {
                IntentSender intentSender = Drive.DriveApi
                        .newOpenFileActivityBuilder()
                        .setMimeType(
                                new String[] { "text/plain", "text/html",
                                        "application/pdf", "audio/mp3",
                                        "video/mp4", "text/plain",
                                        "application/msword", "image/jpeg",
                                        "image/png", "text/html" })
                        .build(getGoogleApiClient());
                try {
                    startIntentSenderForResult(intentSender, REQUEST_CODE_OPENER,
                            null, 0, 0, 0);
                } catch (IntentSender.SendIntentException e) {
                    Log.w(TAG, "Unable to send intent", e);
                }
            } else {
                DriveFile file = Drive.DriveApi.getFile(getGoogleApiClient(),
                        mFileId);
                Log.i("File Name is ", "===========>>>>" + file);
                file.getMetadata(getGoogleApiClient()).setResultCallback(
                        metadataCallback);
            }
        }

        /**
         * Handles response from the file picker.
         */
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {
                case REQUEST_CODE_OPENER:
                    if (resultCode == RESULT_OK) {
                        mFileId = (DriveId) data
                                .getParcelableExtra(OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);

                    } else {
                        finish();
                    }
                    break;
                default:
                    super.onActivityResult(requestCode, resultCode, data);
            }
        }

        /**
         * Handles the metadata response. If file is pinnable and not already
         * pinned, makes a request to pin the file.
         */
        final ResultCallback<DriveResource.MetadataResult> metadataCallback = new ResultCallback<DriveResource.MetadataResult>() {
            @Override
            public void onResult(DriveResource.MetadataResult result) {
                if (!result.getStatus().isSuccess()) {
                    showMessage("Problem while trying to retrieve the file metadata");
                    return;
                }
                if (result.getMetadata().isPinnable()) {
                    showMessage("File is not pinnable");
                    Log.i("==========All Info is here", "mIME tYPE=====>>>"
                            + result.getMetadata().getMimeType() + "======>"
                            + result.getMetadata().getTitle() + "====="

                            + result.getMetadata().getAlternateLink() + "======"

                            + result.getMetadata().getWebContentLink() + "======>>"
                            + result.getMetadata().getQuotaBytesUsed());
                    return;
                }
                if (result.getMetadata().isPinned()) {
                    showMessage("File is already pinned");
                    return;
                }

                Log.i("==========All Info is here", "==========>>>");

                DriveFile file = Drive.DriveApi.getFile(getGoogleApiClient(),
                        mFileId);
                MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                        .setPinned(true).build();
                file.updateMetadata(getGoogleApiClient(), changeSet)
                        .setResultCallback(pinningCallback);

            }
        };

        /**
         * Handles the pinning request's response.
         */
        final ResultCallback<DriveResource.MetadataResult> pinningCallback = new ResultCallback<DriveResource.MetadataResult>() {
            @Override
            public void onResult(DriveResource.MetadataResult result) {
                if (!result.getStatus().isSuccess()) {
                    showMessage("Problem while trying to pin the file");
                    return;
                }
                showMessage("File successfully pinned to the device");
            }
        };
    }
我只获得用户选择弹出窗口

在显示此弹出窗口后选择用户,并显示如何修复此弹出窗口。 我得到这个错误:

ConnectionResult{statusCode=需要在中签名, 分辨率=pendingent{2af2f49:android.os。BinderProxy@462504a}}


你的代码是正确的。您确定您的
文件
引用(即file.open)已正确初始化且不为空吗?在代码中设置断点,并检查引发错误的区域中是否存在空值。我初始化正确的file=new DriveFile(),我认为这不是创建文件的正确方法。也请参阅文档,您的问题标题是如何读取文件,但您的代码正在以“仅写”方式打开文件。如果我更改了该文件,则您的代码是正确的。您确定您的
文件
引用(即file.open)已正确初始化且不为空吗?在代码中设置断点,并检查引发错误的区域中是否存在空值。我初始化正确的file=new DriveFile(),我认为这不是创建文件的正确方法。请参阅文档,您的问题标题是如何读取文件,但您的代码正在以“仅写”的方式打开文件。如果我更改了该文件,请参阅文档
public abstract class BaseDemoActivity extends Activity implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

    private static final String TAG = "BaseDriveActivity";

    /**
     * DriveId of an existing folder to be used as a parent folder in
     * folder operations samples.
     */
    public static final String EXISTING_FOLDER_ID = "0B2EEtIjPUdX6MERsWlYxN3J6RU0";

    /**
     * DriveId of an existing file to be used in file operation samples..
     */
    public static final String EXISTING_FILE_ID = "0ByfSjdPVs9MZTHBmMVdSeWxaNTg";

    /**
     * Extra for account name.
     */
    protected static final String EXTRA_ACCOUNT_NAME = "account_name";

    /**
     * Request code for auto Google Play Services error resolution.
     */
    protected static final int REQUEST_CODE_RESOLUTION = 1;

    /**
     * Next available request code.
     */
    protected static final int NEXT_AVAILABLE_REQUEST_CODE = 2;

    /**
     * Google API client.
     */
    private GoogleApiClient mGoogleApiClient;

    /**
     * Called when activity gets visible. A connection to Drive services need to
     * be initiated as soon as the activity is visible. Registers
     * {@code ConnectionCallbacks} and {@code OnConnectionFailedListener} on the
     * activities itself.
     */
    @Override
    protected void onResume() {
        super.onResume();
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Drive.API)
                    .addScope(Drive.SCOPE_FILE)
                    .addScope(Drive.SCOPE_APPFOLDER) // required for App Folder sample
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
        }
        mGoogleApiClient.connect();
    }

    /**
     * Handles resolution callbacks.
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == RESULT_OK) {
            mGoogleApiClient.connect();
        }
    }

    /**
     * Called when activity gets invisible. Connection to Drive service needs to
     * be disconnected as soon as an activity is invisible.
     */
    @Override
    protected void onPause() {
        if (mGoogleApiClient != null) {
            mGoogleApiClient.disconnect();
        }
        super.onPause();
    }

    /**
     * Called when {@code mGoogleApiClient} is connected.
     */
    @Override
    public void onConnected(Bundle connectionHint) {
        Log.i(TAG, "GoogleApiClient connected");
    }

    /**
     * Called when {@code mGoogleApiClient} is disconnected.
     */
    @Override
    public void onConnectionSuspended(int cause) {
        Log.i(TAG, "GoogleApiClient connection suspended");
    }

    /**
     * Called when {@code mGoogleApiClient} is trying to connect but failed.
     * Handle {@code result.getResolution()} if there is a resolution is
     * available.
     */
    @Override
    public void onConnectionFailed(ConnectionResult result) {
        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;
        }
        try {
            result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
        } catch (IntentSender.SendIntentException e) {
            Log.e(TAG, "Exception while starting resolution activity", e);
        }
    }

    /**
     * Shows a toast message.
     */
    public void showMessage(String message) {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }

    /**
     * Getter for the {@code GoogleApiClient}.
     */
    public GoogleApiClient getGoogleApiClient() {
        return mGoogleApiClient;
    }
}