如何使用保管箱 api v2 使用 android 仅获取保管箱中 .txt 文件的内容

How to get only the contents of a .txt file in dropbox using the dropbox api v2 using android

我想获取保管箱中文件的内容。在互联网上,我只找到了下载文件的选项。但它给了我找不到文件的异常。

    java.io.FileNotFoundException: /storage/emulated/0/Download/Temp/test.txt (No such file or directory)
        at java.io.FileOutputStream.open(Native Method)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
        at com.codeyard.teleprompter.DownloadFileTask.doInBackground(DownloadFileTask.java:72)
        at com.codeyard.teleprompter.DownloadFileTask.doInBackground(DownloadFileTask.java:23)
        at android.os.AsyncTask.call(AsyncTask.java:305)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:243)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:762)
2019-11-26 13:25:59.512 15240-15240/com.codeyard.teleprompter E/ViewRootImpl: sendUserActionEvent() returned.

我怎样才能只获得内容而不是完全下载文件

package com.codeyard.teleprompter;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;

import com.dropbox.core.DbxException;
import com.dropbox.core.v2.DbxClientV2;
import com.dropbox.core.v2.files.FileMetadata;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Task to download a file from Dropbox and put it in the Downloads folder
 */
class DownloadFileTask extends AsyncTask<String, Void, File> {

    private static final String TAG = "TELEPROMPTER";
    @SuppressLint("StaticFieldLeak")
    private final Context mContext;
    private final DbxClientV2 mDbxClient;
    private final Callback mCallback;
    private Exception mException;

    public interface Callback {
        void onDownloadComplete(File result);
        void onError(Exception e);
    }

    DownloadFileTask(Context context, DbxClientV2 dbxClient, Callback callback) {
        mContext = context;
        mDbxClient = dbxClient;
        mCallback = callback;
    }

    @Override
    protected void onPostExecute(File result) {
        super.onPostExecute(result);
        if (mException != null) {
            mCallback.onError(mException);
        } else {
            mCallback.onDownloadComplete(result);
        }
    }

    @Override
    protected File doInBackground(String... params) {
        String metadata = params[0];
        try {
            File path = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DOWNLOADS);
            File file = new File(path, metadata);

            // Make sure the Downloads directory exists.
            if (!path.exists()) {
                if (!path.mkdirs()) {
                    mException = new RuntimeException("Unable to create directory: " + path);
                }
            } else if (!path.isDirectory()) {
                mException = new IllegalStateException("Download path is not a directory: " + path);
                return null;
            }

             //Download the file.
            try (OutputStream outputStream = new FileOutputStream(file)) {
                mDbxClient.files().download(metadata.toLowerCase())
                        .download(outputStream);
            }

            // Tell android about the file
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            intent.setData(Uri.fromFile(file));
            mContext.sendBroadcast(intent);

            return file;
        } catch (DbxException | IOException e) {
            mException = e;
        }

        return null;
    }
}

也许有任何方法可以从 dropbox api v2 调用??? 我想要的是只有文本内容像字符串或其他东西。代码来自官方 github repo

你的问题与下载无关,只是机智的陈述

(OutputStream outputStream = new FileOutputStream(file))  

但是您可以打开 ByteArrayOutputStream 而不是 FIleOutputStream。

通过这种方式,您可以将所有字节都保存在内存中,而不必保存。