我正在研究xamarin.android并使用Marshmallow设备。我使用Intent打开文件,选择最多1200个文件工作正常,但是当我选择1475个文件时,它会挂起,当我调试它时,它也会关闭。
这是意图,我用过
var Intent = new Intent();
Intent.SetType("image/*");
Intent.PutExtra(Intent.ExtraAllowMultiple, true);
Intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent, 2);
我的应用程序不应该有任何添加文件的限制。 Android设备有任何限制吗?
纠正我,我做错了
编辑:
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));
}
}
}
答案 0 :(得分:1)
Android OS中可能存在一些限制,因为它们不支持大量的URI。好吧,我找到了另一种解决方案,如果有人需要,这就是步骤:
DocumentTree
打开Intent
。OnActivityResult
我们有一个URI(TreeUri)。通过上述URI中的文件循环,我们可以获取所有相应的文件
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
}
以上步骤要求用户使用DocumentTree
选择文件夹,现在我可以获取所需的文件。我测试了大约7000个文件,运行顺利。
答案 1 :(得分:0)
意图没有大容量。您可以使用全局变量,共享首选项或临时文件。
Maximum length of Intent putExtra method? (Force close)
https://www.neotechsoftware.com/blog/android-intent-size-limit
临时文件:Creating temporary files in Android
共享偏好设置:https://www.tutorialspoint.com/android/android_shared_preferences.htm
答案 2 :(得分:0)
我的问题已解决,可能是Visual Studio 2017社区版中的错误或我在项目中使用的一些参考dll。
我从以下版本降级版本:
Xamarin.Android.Support.Compat 26.1.0.1
到
Xamarin.Android.Support.Compat.25.4.0.2
包含所有依赖项并再次重新升级并重新生成项目,现在我在添加无限文件方面没有任何问题。