1

I am trying to migrate my Android app to Storage Access Framework in case they deprecate file level access to the user's device storage (as it seems).

My app has to unpack a zip file and the following code is intended to perform that operation:

private boolean unpackZip(String targetPath, String zipFilePathUri //zipFilePath)
{       
     InputStream inputStream;
     ZipInputStream zInputStream;
     try 
     {
         inputStream =getContentResolver().openInputStream(Uri.parse(zipFilePathUri));

         //here's the old code
         //inputStream = new FileInputStream(zipFilePath); 
         //ZipFile zfile=new ZipFile(inputStream);

         int maxEntries;
         File mainfmd = new File(targetPath);
         zInputStream = new ZipInputStream(new BufferedInputStream(inputStream));
         ZipEntry zipEntry;
         int entriesCounter=0;
         while ((zipEntry = zInputStream.getNextEntry()) != null)
    ....
    ....

How it can be noticed there is a different implementation when SAF comes into place. I experienced very bad performance when using getContentResolver().openInputStream, indeed I see that the real unpacking starts after a long pause in the app functioning, while it was very quick before. Am I wrong at believing that?

P5music
  • 3,197
  • 2
  • 32
  • 81
  • how do you `"see that the real unpacking starts after a long pause"`? did you try to add some `Log.d` after `openInputStream()`, after `new ZipInputStream` and inside the while loop? if so, when are they shown? after what delays? – pskink Sep 18 '19 at 08:38
  • @pskink the while loop updates a progress bar, and I compare with the previous version of the app, I notice a very long pause in the SAF version before the progress starts. – P5music Sep 18 '19 at 08:45
  • is that `while` loop called in some background thread? if so, how do you pass the current progress? – pskink Sep 18 '19 at 08:46
  • @pskink it is a service, the progress value is broadcasted – P5music Sep 18 '19 at 08:48
  • is that `while` loop called in some background thread? are you using `IntentService` for example? the docs say: *"IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through Context.startService(Intent) calls; the service is started as needed, handles each Intent in turn using a **worker thread**, and stops itself when it runs out of work."* – pskink Sep 18 '19 at 08:51
  • @pskink yes it is so, it worked very smoothly, now something changed with SAF – P5music Sep 18 '19 at 08:59
  • ok so first try to add those `Log.d` and watch the logcat and check if input stream processing is really the bottleneck here – pskink Sep 18 '19 at 09:00
  • @pskink It seems that the entire loop is performed, then the progress bar starts to be updated, but it is progressing gradually as it should do. What is the problem? – P5music Sep 18 '19 at 09:14
  • your broadcasts, use `Handler`s instead, more: https://developer.android.com/training/multiple-threads/communicate-ui – pskink Sep 18 '19 at 09:15
  • @pskink why does it happen only with the new SAF version? – P5music Sep 18 '19 at 09:20
  • i have no idea why – pskink Sep 18 '19 at 09:20

0 Answers0