You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AWS Amplify从S3下载文件后,Android Studio中如何读取?

How to Access Your Downloaded Amplify S3 File (Text & JSON)

Hey there! I totally get the confusion here—let's clear this up for you right away.

First: Where's Your Downloaded File?

In your code, you're saving the file to getApplicationContext().getFilesDir() + "/download.txt"—this points to your app's internal storage directory, which is private to your app (only your app can access it). This is why you don't see it in your project files on your computer: it's stored directly on the Android device/emulator running your app.

The full path looks something like this:
/data/data/[your-app-package-name]/files/download.txt

To view it manually:

  • Open Android Studio's Device File Explorer (usually in the bottom-right corner)
  • Navigate to the path above (you'll need to unlock the data folder if using an emulator)

Next: How to Read the Text File Content

Since you're getting a File object in the success callback, you can immediately read its content using standard Java file-reading utilities. Here's how to modify your existing code to read the text:

Amplify.Storage.downloadFile(
    "myfile.txt",
    new File(getApplicationContext().getFilesDir() + "/download.txt"),
    result -> {
        Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName());
        
        // Read the text file content
        File downloadedFile = result.getFile();
        try (BufferedReader reader = new BufferedReader(new FileReader(downloadedFile))) {
            StringBuilder content = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line).append("\n");
            }
            Log.i("MyAmplifyApp", "File content: " + content.toString());
            // Do whatever you need with the content here
        } catch (IOException e) {
            Log.e("MyAmplifyApp", "Failed to read file", e);
        }
    },
    error -> Log.e("MyAmplifyApp", "Download Failure", error)
);

For JSON Files Later

When you download a JSON file, the process is almost identical—you just need to parse the text content into a JSON object/array. Here are two common approaches:

Using Android's Built-in JSONObject

// After reading the content string as shown above
try {
    JSONObject jsonObject = new JSONObject(content.toString());
    // Access JSON values: jsonObject.getString("key"), jsonObject.getInt("number"), etc.
    Log.i("MyAmplifyApp", "Parsed JSON: " + jsonObject.toString());
} catch (JSONException e) {
    Log.e("MyAmplifyApp", "Failed to parse JSON", e);
}

First add Gson to your app's build.gradle file:

implementation 'com.google.code.gson:gson:2.10.1'

Then parse the content:

Gson gson = new Gson();
// Replace YourDataModel with a custom class matching your JSON structure
YourDataModel dataModel = gson.fromJson(content.toString(), YourDataModel.class);
// Now you can access fields directly, e.g. dataModel.getUsername()

That's all! You're already halfway there—just adding this reading/parsing logic to your success callback will let you work with the file content seamlessly.

内容的提问来源于stack exchange,提问作者Brian16446

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 06:43:13