Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.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_Google Drive Api_Google Drive Android Api - Fatal编程技术网

从android上传谷歌硬盘图像

从android上传谷歌硬盘图像,android,google-drive-api,google-drive-android-api,Android,Google Drive Api,Google Drive Android Api,我试图上传一个图像到谷歌驱动器使用API。我找了这么多,但没找到办法。我有一个演示代码,上传一个文本文件,它的工作。我需要将其更改为上载图像。 这是代码 public void CreateFileOnGoogleDrive(DriveContentsResult result){ final DriveContents driveContents = result.getDriveContents(); // Perform I/O off the UI thread.

我试图上传一个图像到谷歌驱动器使用API。我找了这么多,但没找到办法。我有一个演示代码,上传一个文本文件,它的工作。我需要将其更改为上载图像。 这是代码

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();
}
如何更改此代码以从文件上载图像。(根据设备中图像的给定位置)。?
我发现很少有教程,但这些都是不推荐的方法。

首先,您不需要创建一个文件,然后将内容写入其中

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");
                    }
                }
            });
}
private void saveFiletoDrive(最终文件,最终字符串mime){
Drive.DriveApi.newDriveContents(mDriveClient.setResultCallback)(
新的ResultCallback(){
@凌驾
public void onResult(DriveContentsResult结果){
如果(!result.getStatus().issucess()){
Log.i(标记“未能创建新内容”);
返回;
}
i(标记“连接成功,创建新内容…”);
OutputStream OutputStream=result.getDriveContents()
.getOutputStream();
文件输入流fis;
试一试{
fis=新文件输入流(file.getPath());
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
字节[]buf=新字节[1024];
int n;
而(-1!=(n=fis.read(buf)))
写入(buf,0,n);
byte[]photoBytes=bas.toByteArray();
outputStream.write(photoBytes);
outputStream.close();
outputStream=null;
fis.close();
fis=null;
}catch(filenotfounde异常){
w(标记“FileNotFoundException:+e.getMessage());
}捕获(IOE1异常){
Log.w(标记“无法写入文件内容”。+e1.getMessage());
}
字符串title=file.getName();
MetadataChangeSet MetadataChangeSet=新建MetadataChangeSet.Builder()
.setMimeType(mime).setTitle(title.build();
if(mime.equals(mime_PHOTO)){
如果(详细)
Log.i(标签,“在驱动器上创建新照片”(+标题
+ ")");
Drive.DriveApi.getFolder(mDriveClient,
mPicFolderDriveId).createFile(mDriveClient,
metadataChangeSet,
result.getDriveContents());
}else if(mime.equals(mime_视频)){
Log.i(标签,“在驱动器上创建新视频”(“+标题
+ ")");
Drive.DriveApi.getFolder(mDriveClient,
mVidFolderDriveId).createFile(mDriveClient,
metadataChangeSet,
result.getDriveContents());
}
if(file.delete()){
如果(详细)
Log.d(标记“从SD卡中删除”+文件.getName()+”);
}否则{
Log.w(标记“未能从SD卡中删除”+file.getName()+”);
}
}
});
}

首先,您不需要创建一个文件,然后将内容写入其中

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");
                    }
                }
            });
}
private void saveFiletoDrive(最终文件,最终字符串mime){
Drive.DriveApi.newDriveContents(mDriveClient.setResultCallback)(
新的ResultCallback(){
@凌驾
public void onResult(DriveContentsResult结果){
如果(!result.getStatus().issucess()){
Log.i(标记“未能创建新内容”);
返回;
}
i(标记“连接成功,创建新内容…”);
OutputStream OutputStream=result.getDriveContents()
.getOutputStream();
文件输入流fis;
试一试{
fis=新文件输入流(file.getPath());
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
字节[]buf=新字节[1024];
int n;
而(-1!=(n=fis.read(buf)))
写入(buf,0,n);
byte[]photoBytes=bas.toByteArray();
outputStream.write(photoBytes);
outputStream.close();
outputStream=null;
fis.close();
fis=null;
}catch(filenotfounde异常){
w(标记“FileNotFoundException:+e.getMessage());
}捕获(IOE1异常){
Log.w(标记“无法写入文件内容”。+e1.getMessage());
}
字符串title=file.getName();
MetadataChangeSet MetadataChangeSet=新建MetadataChangeSet.Builder()
.setMimeType(mime).setTitle(title.build();
if(mime.equals(mime_PHOTO)){
如果(详细)
Log.i(标签,“在驱动器上创建新照片”(+标题
+ ")");
Drive.DriveApi.getFolder(mDriveClient,
mPicFolderDriveId).createFile(mDriveClient,
metadataChangeSet,
result.getDriveCont