2

I am working on xamarin.android and use Marshmallow device. I use Intent to open files and selecting up to 1200 files is working fine but when I select 1475 files, it hangs and when I debug this, it also closed.

Here is the Intent, I had used

var Intent = new Intent();
Intent.SetType("image/*");
Intent.PutExtra(Intent.ExtraAllowMultiple, true);
Intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent, 2);

My application should not have any limitation of adding files. Is there any limitation in android device?

Do correct me, where I am doing wrong

Edit:

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
           if ((requestCode == 2) && (resultCode == Result.Ok) && (data != null))
            {
                //------------- taking permission for the opened uri
                var takeFlags = ActivityFlags.GrantReadUriPermission | ActivityFlags.GrantWriteUriPermission;
                // Check for the freshest data.
                try
                {
                    if (data.Data != null)
                    {
                        GrantUriPermission(PackageName, data.Data, ActivityFlags.GrantReadUriPermission | ActivityFlags.GrantWriteUriPermission);
                        ContentResolver.TakePersistableUriPermission(data.Data, takeFlags);
                    }
                }
                catch { }

                var ar = data.ClipData;
                if (ar != null)
                {
                    clipDataImages = ar;
                    // in the below activity, I had used separate thread for loading to Recycler view, but it did not start the LoadImageActivity
                    StartActivity(typeof(LoadImageActivity));
                }
    }
}
Amit
  • 264
  • 1
  • 4
  • 18
  • There is no limitation but, you can not hold main thread for long time thats why your application is getting close, in this kind of situation we have to use other thread or we have to user some background task. – Lovekush Vishwakarma Mar 27 '18 at 13:49
  • 1
    I would really re-think any design that requires you to open this many files at once, particularly on a mobile device. – Jason Mar 27 '18 at 14:04
  • @Lovekush Vishwakarma I had updated my question, I cannot figure out the problem – Amit Mar 27 '18 at 14:15
  • @Jason In this activity, I only used to add URI to the list. I was only using Intent to add files. After all the files get loaded, the next step is to process using a background thread. However, when I add the whole list as 1200+ next 1200, it loads perfectly but it can't handle 1475 files all at once. – Amit Mar 27 '18 at 14:25

3 Answers3

1

There might be some restrictions in Android OS for not supporting large quantity of URI's. Well I had found an alternative solution, here's the step if someone's required:

  1. Open DocumentTree through Intent.
  2. OnActivityResult we got an URI (TreeUri) in it.
  3. Looping though the files from the above URI, we can fetch all the respective files

    var dirTree = DocumentFile.FromTreeUri(this, TreeUri); var files = dirTree.ListFiles(); for (int x = 0; x < files.Length; x++) { var SingleFileURI = files[x].Uri; // doing stuff with SingleFileURI }

The above steps is asking user to select a folder using DocumentTree and now I can fetch as much files we want. I had test approx 7000 files, which runs smoothly.

Amit
  • 264
  • 1
  • 4
  • 18
0

intent does not have a large capacity. You can use global variables, shared preferences or temp files for this.

Maximum length of Intent putExtra method? (Force close)

https://www.neotechsoftware.com/blog/android-intent-size-limit

Temp files: Creating temporary files in Android

Shared Preferences : https://www.tutorialspoint.com/android/android_shared_preferences.htm

global variable: Android global variable

mesutpiskin
  • 1,771
  • 2
  • 26
  • 30
  • I am starting Intent, so that user can pick files from the favorite path. Is their any flags or category that I have to pass with Intent to handle files without hanging or limitation. – Amit Mar 27 '18 at 14:29
0

I got my issue resolved, it might be bug in visual studio 2017 community edition or some of the reference dll that I had used in project.

I degrade the version from:

Xamarin.Android.Support.Compat 26.1.0.1

to

Xamarin.Android.Support.Compat.25.4.0.2

with all dependencies and again re-upgrade it and rebuild the project again and now I have no issue in adding unlimited files.

Amit
  • 264
  • 1
  • 4
  • 18
  • After downgrading the version, I am able to select upto 1802 files using the Intent. I don't able to figure out the actual issue. Has anyone faced this issue or have any idea to tackle this. – Amit Mar 31 '18 at 09:27