Android Question How to perform resource management while batch copying files?

Discussion in 'Android Questions' started by Inman, Jan 12, 2019.

Tags:
  1. Inman

    Inman Well-Known Member Licensed User

    In my app I have a For Next loop that is used to copy all files in a particular folder. Since the target location is a URI, I use the method given here to copy the files. And it works correctly too.

    The problem arises when there are a lot of files to copy. Like there is a user who tried to copy about 3000 files in a folder. He reported that every time he tried, the loop copies a few files and then goes to stay in "not responding" mode. He then force stops it, tries again, copies a few more and then "not responding".

    I am wondering if this has something to do with the app running out of resources. I have tried the same with like 10 to 50 files and it never had any issues. But apparently with 1000+ files it hangs.

    So is there a right way to lock and release resources after copying each file?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    The issue is not related to resource management or locks. You need to use File.CopyAsync or Copy2Async to make the copy without blocking the main thread.
     
  3. Inman

    Inman Well-Known Member Licensed User

    Thanks Erel. I will make this change in the activity's code.

    Besides activity, I also perform similar copy operation in a service as well using File.Copy2. Since there is no UI thread here, is it necessary to change this Copy2 to Copy2Async?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Yes. It doesn't matter whether the code is in a service or an activity.
     
    Inman likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice