Android的驱动API无效的父文件夹的错误 [英] Android Drive API Invalid parent folder error

查看:266
本文介绍了Android的驱动API无效的父文件夹的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的Andr​​oid驱动的API来上传我的文件上Drive.I在谷歌驱动器根目录创建一个文件夹(NewFolder),并使用以下$ C从Android应用程序内部创建了我的NewFolder一个内部文件夹(InnerFolder) $ c.Then我写我的文件从我的Andr​​oid应用这种InnerFolder。

我的应用我的设备上创建第一次安装了InnerFolderonly.Then卸载并重新安装应用程序不会再创建的文件夹(存储创建的文件夹的驱动器ID(如驱动器Id:yrjundeen12cnfe)的本地文件)。

我的code正常工作的第一次Installation.But如果我卸载并重新安装应用程序很多次,我尝试在谷歌驱动器上传,我得到了错误作为的CreateFile的回调方法无效的父文件夹。

第一次创建文件夹code:

 私人无效createGalleryFolder(){
        DRIVEFOLDER文件夹= Drive.DriveApi.getRootFolder(googleApiClient);
        MetadataChangeSet变更=新MetadataChangeSet.Builder()
                .setTitle(AppConstants.DEFAULT_GALLERY_NAME).build();
        folder.createFolder(googleApiClient,变更).setResultCallback(
                createRootFolderCallback);
}        最后ResultCallback< D​​riveFolderResult> createRootFolderCallback =新ResultCallback< D​​riveFolderResult>(){    @覆盖
    公共无效onResult(DriveFolderResult结果){
        如果(!result.getStatus()。isSuccess()){
            showError();
            返回;
        }        //根文件夹创建成功
        驱动器Id = mFolderDriveId result.getDriveFolder()getDriveId()。        AppUtilities.saveOnLocalFile(mFolderDriveId,AppConstants.ROOT_FOLDER_ID_FILE);        如果(!deviceID.equalsIgnoreCase()){
            DRIVEFOLDER文件夹= Drive.DriveApi.getFolder(googleApiClient,
                    result.getDriveFolder()getDriveId())。
            MetadataChangeSet变更=新MetadataChangeSet.Builder()
                    .setTitle(设备ID).build();
            folder.createFolder(googleApiClient,变更)
                    .setResultCallback(createInnerFolderCallback);
        }其他{            showError(); }    }
};最后ResultCallback< D​​riveFolderResult> createInnerFolderCallback =新ResultCallback< D​​riveFolderResult>(){    @覆盖
    公共无效onResult(DriveFolderResult结果){
        如果(!result.getStatus()。isSuccess()){            showError();
            返回;
        }        根文件夹内创建成功// IMEI文件夹
        驱动器Id = mFolderDriveId result.getDriveFolder()getDriveId()。        AppUtilities.saveOnLocalFile(mFolderDriveId,AppConstants.INNER_FOLDER_ID_FILE);        //成功
    }
};

文件上传code:

 私人无效saveFileToDrive(最终位图bitmapToSave,最后的文件镜像文件,最后弦乐imageName){        Drive.DriveApi.newDriveContents(googleApiClient).setResultCallback(driveContentsCallback);
}最后的私人ResultCallback< D​​riveContentsResult> driveContentsCallback =
            新ResultCallback< D​​riveContentsResult>(){
        @覆盖
        公共无效onResult(DriveContentsResult结果){            如果(!result.getStatus()。isSuccess()){
                Log.i(测试1,无法创建新的内容);
                返回;
            }            Log.i(测试1,新创作的作品);            //写入图像数据的OutputStream
            为OutputStream的OutputStream = result.getDriveContents()的getOutputStream()。
            ByteArrayOutputStream bitmapStream =新ByteArrayOutputStream();
            bitmapToSave.com preSS(Bitmap.Com pressFormat.JPEG,100,bitmapStream);
            尝试{
                outputStream.write(bitmapStream.toByteArray());                DRIVEFOLDER文件夹= Drive.DriveApi.getFolder(googleApiClient,DriveId.de codeFromString(getFolderDriveID()));
                MetadataChangeSet变更=新MetadataChangeSet.Builder()
                        .setTitle(imageName)
                        .setMimeType(图像/ JPEG)
                        .setStarred(假).build();                folder.createFile(googleApiClient,变更,result.getDriveContents())setResultCallback(fileCallback)。
            }赶上(IOException异常五){
                Log.i(测试1,无法写入文件的内容);
            }
        }
    };    最后的私人ResultCallback< D​​riveFileResult> fileCallback =
            新ResultCallback< D​​riveFileResult>(){
        @覆盖
        公共无效onResult(DriveFileResult结果){
            如果(!result.getStatus()。isSuccess()){
                Log.i(测试1,错误试图创建文件+ result.getStatus()getStatusMessage());
                返回;
            }
            Log.i(Test1的,创建一个文件:+ result.getDriveFile()getDriveId());            result.getDriveFile()的getMetaData(googleApiClient).setResultCallback(fileUploadedResultCallback)。
        }
    };
    最后的私人ResultCallback< MetadataResult> fileUploadedResultCallback =
            新ResultCallback< MetadataResult>(){
        @覆盖
        公共无效onResult(MetadataResult结果){            如果(!result.getStatus()。isSuccess()){
                Log.i(测试1,无法上传);
                返回;
            }            Log.i(测试1,文件上传);
        }
    };

上ResultCallback错误:

  D / dalvikvm(584):主题ID = 50:插值堆在0x549a5000
D / dalvikvm(584):主题ID = 50:调用的run()
I /(813)的Test1:图像添加
---------的/ dev /日志/系统开始
D / ActivityThread(584):SVC-CREATE_SERVICE处理:0 / CreateServiceData{token=android.os.BinderProxy@415dbf40的className = com.google.android.gms.drive.api.DriveAsyncService的packageName = com.google.android.gms意图= NULL}
D / ActivityThread(584):SVC-调用onStartCommand:com.google.android.gms.drive.api.DriveAsyncService@415b6480,旗帜= 2,startId = 1
D / ActivityThread(584):SVC-SERVICE_ARGS处理:0 / ServiceArgsData{token=android.os.BinderProxy@415dbf40 startId = 1 ARGS = {意图行事= com.google.android.gms.drive.EXECUTE PKG = com.google .android.gms}}
W / DataServiceConnectionImpl(584):找不到入口,也没有有效的资源ID:驱动器Id:CAESABgGIMq7nNDgUg ==
E / DriveAsyncService(584):无效的父文件夹。
E / DriveAsyncService(584):OperationException [状态{状态code =无效的父文件夹,分辨率= NULL}]
E / DriveAsyncService(584):在com.google.android.gms.drive.api.e.e(的SourceFile:619)
E / DriveAsyncService(584):在com.google.android.gms.drive.api.e.a(的SourceFile:458)
E / DriveAsyncService(584):在com.google.android.gms.drive.api.a.n.a(的SourceFile:82)
E / DriveAsyncService(584):在com.google.android.gms.drive.api.a.b.a(的SourceFile:27)
E / DriveAsyncService(584):在com.google.android.gms.common.service.c.onHandleIntent(的SourceFile:60)
E / DriveAsyncService(584):在android.app.IntentService $ ServiceHandler.handleMessage(IntentService.java:65)
E / DriveAsyncService(584):在android.os.Handler.dispatchMessage(Handler.java:99)
E / DriveAsyncService(584):在android.os.Looper.loop(Looper.java:154)
E / DriveAsyncService(584):在android.os.HandlerThread.run(HandlerThread.java:65)
I / Test1的(813):错误尝试创建该文件无效的父文件夹。


解决方案

不要依赖于 GDAA的 驱动器Id 的任何缓存或可能解决驱动对象(文件/文件夹)超出应用程序和设备的范围之内。这个ID是从谷歌的设备上播放Services实例来(所以不是即使使用相同的应用程序在不同的设备上有效的 - 即使文件/文件夹仍然可见下的文件范围)。我依稀记得清谷歌播放服务(GooPS)高速缓存,甚至重新安装GooPS时,失去它甚至。但是,这可能已得到修复。我与GDAA前遭遇是在几个月前。

相反,有一个资源ID,即在 REST风格的API使用 (被称为纯'ID') - 见 SO 21800257 。这个字符串唯一重新presents Drive对象,并可以移植,并到处被使用(它是驱动Web界面HTTP地址,甚至一部分)。结果
不幸的是,资源ID不能立即可用(直到GDAA提交的对象) - <一个href=\"http://stackoverflow.com/questions/22432431/google-drive-android-api-gdaa-getresourceid-returning-null-timing-issue\">SO 22432431

好运

I used Android Drive Api to upload my files on Drive.I created a folder("NewFolder") on Google Drive root and created a inner folder("InnerFolder") inside my "NewFolder" from android app using following code.Then I write my files on this "InnerFolder" from my android app.

My app create the "InnerFolder" on first time installation on my device only.Then uninstall and reinstall App will not created folder again(Stored created folder Drive Id(like DriveId:yrjundeen12cnfe) in local file).

My Code works fine on first time Installation.But If I uninstall and reinstall app many times and I try to upload on Google Drive,I got error as "Invalid parent folder" on callback method of createFile.

First Time Folder Creation code:

private void createGalleryFolder() {
        DriveFolder folder = Drive.DriveApi.getRootFolder(googleApiClient);
        MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                .setTitle(AppConstants.DEFAULT_GALLERY_NAME).build();
        folder.createFolder(googleApiClient, changeSet).setResultCallback(
                createRootFolderCallback);
}

        final ResultCallback<DriveFolderResult> createRootFolderCallback = new ResultCallback<DriveFolderResult>() {

    @Override
    public void onResult(DriveFolderResult result) {
        if (!result.getStatus().isSuccess()) {
            showError();
            return;
        }

        // Root folder created Successfully
        DriveId mFolderDriveId = result.getDriveFolder().getDriveId();

        AppUtilities.saveOnLocalFile(mFolderDriveId, AppConstants.ROOT_FOLDER_ID_FILE);

        if (!deviceID.equalsIgnoreCase("")) {
            DriveFolder folder = Drive.DriveApi.getFolder(googleApiClient,
                    result.getDriveFolder().getDriveId());
            MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                    .setTitle(deviceID).build();
            folder.createFolder(googleApiClient, changeSet)
                    .setResultCallback(createInnerFolderCallback);
        } else {

            showError();        }

    }
};

final ResultCallback<DriveFolderResult> createInnerFolderCallback = new ResultCallback<DriveFolderResult>() {

    @Override
    public void onResult(DriveFolderResult result) {
        if (!result.getStatus().isSuccess()) {

            showError();
            return;
        }

        // IMEI folder created successfully inside the Root folder
        DriveId mFolderDriveId = result.getDriveFolder().getDriveId();

        AppUtilities.saveOnLocalFile(mFolderDriveId, AppConstants.INNER_FOLDER_ID_FILE);

        // Success
    }
};

File Upload code:

private void saveFileToDrive(final Bitmap bitmapToSave,final File imageFile,final String imageName) {

        Drive.DriveApi.newDriveContents(googleApiClient).setResultCallback(driveContentsCallback);
}

final private ResultCallback<DriveContentsResult> driveContentsCallback =
            new ResultCallback<DriveContentsResult>() {
        @Override
        public void onResult(DriveContentsResult result) {

            if(!result.getStatus().isSuccess()){
                Log.i("Test1", "Failed to create new contents");
                return;
            }

            Log.i("Test1", "New contents created");

            //Write image data to OutputStream
            OutputStream outputStream = result.getDriveContents().getOutputStream();
            ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
            bitmapToSave.compress(Bitmap.CompressFormat.JPEG, 100, bitmapStream);
            try {
                outputStream.write(bitmapStream.toByteArray());

                DriveFolder folder = Drive.DriveApi.getFolder(googleApiClient, DriveId.decodeFromString(getFolderDriveID()));


                MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                        .setTitle(imageName)
                        .setMimeType("image/jpeg")
                        .setStarred(false).build();

                folder.createFile(googleApiClient, changeSet, result.getDriveContents()).setResultCallback(fileCallback);
            } catch (IOException e) {
                Log.i("Test1","Unable to write file contents");
            }
        }
    };

    final private ResultCallback<DriveFileResult> fileCallback =
            new ResultCallback<DriveFileResult>() {
        @Override
        public void onResult(DriveFileResult result) {
            if (!result.getStatus().isSuccess()) {
                Log.i("Test1","Error while trying to create the file "+result.getStatus().getStatusMessage());
                return;
            }
            Log.i("Test1","Created a file: " + result.getDriveFile().getDriveId());

            result.getDriveFile().getMetadata(googleApiClient).setResultCallback(fileUploadedResultCallback);
        }
    };


    final private ResultCallback<MetadataResult> fileUploadedResultCallback =
            new ResultCallback<MetadataResult>() {
        @Override
        public void onResult(MetadataResult result) {

            if(!result.getStatus().isSuccess()){
                Log.i("Test1", "Failed to upload");
                return;
            }

            Log.i("Test1", "File uploaded");
        }
    };

Error on ResultCallback:

D/dalvikvm(  584): threadid=50: interp stack at 0x549a5000
D/dalvikvm(  584): threadid=50: calling run()
I/Test1   (  813): Image added
--------- beginning of /dev/log/system
D/ActivityThread(  584): SVC-CREATE_SERVICE handled : 0 / CreateServiceData{token=android.os.BinderProxy@415dbf40 className=com.google.android.gms.drive.api.DriveAsyncService packageName=com.google.android.gms intent=null}
D/ActivityThread(  584): SVC-Calling onStartCommand: com.google.android.gms.drive.api.DriveAsyncService@415b6480, flags=2, startId=1
D/ActivityThread(  584): SVC-SERVICE_ARGS handled : 0 / ServiceArgsData{token=android.os.BinderProxy@415dbf40 startId=1 args=Intent { act=com.google.android.gms.drive.EXECUTE pkg=com.google.android.gms }}
W/DataServiceConnectionImpl(  584): Could not find entry, and no valid resource id: DriveId:CAESABgGIMq7nNDgUg==
E/DriveAsyncService(  584): Invalid parent folder.
E/DriveAsyncService(  584): OperationException[Status{statusCode=Invalid parent folder., resolution=null}]
E/DriveAsyncService(  584):     at com.google.android.gms.drive.api.e.e(SourceFile:619)
E/DriveAsyncService(  584):     at com.google.android.gms.drive.api.e.a(SourceFile:458)
E/DriveAsyncService(  584):     at com.google.android.gms.drive.api.a.n.a(SourceFile:82)
E/DriveAsyncService(  584):     at com.google.android.gms.drive.api.a.b.a(SourceFile:27)
E/DriveAsyncService(  584):     at com.google.android.gms.common.service.c.onHandleIntent(SourceFile:60)
E/DriveAsyncService(  584):     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
E/DriveAsyncService(  584):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/DriveAsyncService(  584):     at android.os.Looper.loop(Looper.java:154)
E/DriveAsyncService(  584):     at android.os.HandlerThread.run(HandlerThread.java:65)
I/Test1   (  813): Error while trying to create the file Invalid parent folder.

解决方案

Do not rely on GDAA's DriveId for any caching or possibly addressing of Drive object (file/folder) beyond the app and device scope. This ID is coming from Google Play Services instance on your device (so it is not even valid on a different device using the same app - even if file/folder is still visible under the FILE scope). I vaguely remember 'losing' it even when clearing the Google Play Services (GooPS) cache or even re-installing GooPS. But this might have been fixed. My last encounter with GDAA was a few months ago.

Instead, there is a 'ResourceId', that is used in RESTFul API (known as plain 'id') - see SO 21800257. This string uniquely represents the Drive object and can be ported and used everywhere (it is even a part of HTTP address in Drive Web interface).
Unfortunately, ResourceId is not immediately available (until GDAA commits the the object) - SO 22432431

Good Luck

这篇关于Android的驱动API无效的父文件夹的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆