How to move/rename file from internal app storage to external storage on Android?

后端 未结 8 1091
闹比i
闹比i 2020-11-29 02:21

I am downloading files from the internet and saving the streaming data to a temp file in my app\'s internal storage given by getFilesDir().

Once the download is comp

相关标签:
8条回答
  • 2020-11-29 03:02

    After you copy the file (as @barmaley's great answer shows), don't forget to expose it to the device's gallery, so the user can view it later.

    The reason why it has to be done manually is that

    Android runs a full media scan only on reboot and when (re)mounting the SD card

    (as this guide shows).

    The easier way to do this is by sending a broadcast for the scanning to be invoked:

    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    intent.setData(Uri.fromFile(outputFile));
    context.sendBroadcast(intent);
    

    And voila! You can now view your file in the device's gallery.

    0 讨论(0)
  • 2020-11-29 03:13

    You can do it using operations with byte[]

    define in your class:

        public static final String DATA_PATH = 
    Environment.getExternalStorageDirectory().toString() + "/MyAppName/";
    

    then:

    AssetManager assetManager = context.getAssets();
    InputStream in = assetManager.open("data/file.txt");
    
    OutputStream out = new FileOutputStream(DATA_PATH + "data/file.txt");
    
    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
    
    0 讨论(0)
提交回复
热议问题