如何将目录中的所有音频文件上传到谷歌硬盘?

时间:2017-06-26 07:37:33

标签: android google-drive-api

我在将音频列表上传到Google云端硬盘时遇到了问题。

我可以从目录上传单个音频文件,但我试图上传音频文件列表失败。

这是单个音频文件的路径

final String path = new String(Environment.getExternalStorageDirectory() + "/CallLogs/Yaendi Yaendi.mp3");

如何上传CallLogs目录中的所有音频文件。

public void CreateFileOnGoogleDrive(DriveApi.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();

                final String path = new String(Environment.getExternalStorageDirectory() + "/CallLogs/Yaendi Yaendi.mp3");

                FileInputStream inputStream = null;

                try {
                    inputStream = new FileInputStream(new File(path));
                } catch (FileNotFoundException e) {
                    showErrorDialog();
                    e.printStackTrace();
                }

                byte[] buf = new byte[1024];
                int bytesRead;
                try {
                    if (inputStream != null) {
                        while ((bytesRead = inputStream.read(buf)) > 0) {
                            outputStream.write(buf, 0, bytesRead);
                        }
                    }
                } catch (IOException e) {
                    showErrorDialog();
                    e.printStackTrace();
                }

 MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                        .setTitle("callLog")
                        .setMimeType("audio/mpeg")
                        .setStarred(true).build();


                // create a file in root folder
                Drive.DriveApi.getRootFolder(mGoogleApiClient)
                        .createFile(mGoogleApiClient, changeSet, driveContents).setResultCallback(fileCallback);

            }
        }.start();

        Toast.makeText(getActivity(), "Created Successfully", Toast.LENGTH_SHORT).show();
    }

以上代码用于将单个音频文件上传到Google云端硬盘。 请帮我如何将所有文件上传到谷歌硬盘。

1 个答案:

答案 0 :(得分:0)

将您的代码放在AsyncTask的doInBackground()方法中。