簡體   English   中英

如何在 Xamarin Android 中一次(勾選框)從圖庫中選擇多個圖像?

[英]How to choose multiple image from Gallery at one time (tickbox) in Xamarin Android?

您好,有一個使用 C# 的 Xamarin Android 項目。 目前我正在使用 await CrossMedia.Current.PickPhotoAsync() 方法上傳圖像。 但是,它沒有在圖像旁邊提供一個復選框讓我選擇多個。 如何設法選擇多個圖像並一起上傳?

你可以自己實現。

1.將這些方法添加到您的 MainActivity.cs 文件中

    public static int OPENGALLERYCODE = 100;
    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);

        //If we are calling multiple image selection, enter into here and return photos and their filepaths.
        if (requestCode == OPENGALLERYCODE && resultCode == Result.Ok)
        {
            List<string> images = new List<string>();

            if (data != null)
            {
                //Separate all photos and get the path from them all individually.
                ClipData clipData = data.ClipData;
                if (clipData != null)
                {
                    for (int i = 0; i < clipData.ItemCount; i++)
                    {
                        ClipData.Item item = clipData.GetItemAt(i);
                        Android.Net.Uri uri = item.Uri;
                        var path = GetRealPathFromURI(uri);


                        if (path != null)
                        {
                            images.Add(path);
                        }
                    }
                }
                else
                {
                    Android.Net.Uri uri = data.Data;
                    var path = GetRealPathFromURI(uri);

                    if (path != null)
                    {
                        images.Add(path);
                    }
                }


            }
        }
    }

    /// <summary>
    ///     Get the real path for the current image passed.
    /// </summary>
    public String GetRealPathFromURI(Android.Net.Uri contentURI)
    {
        try
        {
            ICursor imageCursor = null;
            string fullPathToImage = "";

            imageCursor = ContentResolver.Query(contentURI, null, null, null, null);
            imageCursor.MoveToFirst();
            int idx = imageCursor.GetColumnIndex(MediaStore.Images.ImageColumns.Data);

            if (idx != -1)
            {
                fullPathToImage = imageCursor.GetString(idx);
            }
            else
            {
                ICursor cursor = null;
                var docID = DocumentsContract.GetDocumentId(contentURI);
                var id = docID.Split(':')[1];
                var whereSelect = MediaStore.Images.ImageColumns.Id + "=?";
                var projections = new string[] { MediaStore.Images.ImageColumns.Data };

                cursor = ContentResolver.Query(MediaStore.Images.Media.InternalContentUri, projections, whereSelect, new string[] { id }, null);
                if (cursor.Count == 0)
                {
                    cursor = ContentResolver.Query(MediaStore.Images.Media.ExternalContentUri, projections, whereSelect, new string[] { id }, null);
                }
                var colData = cursor.GetColumnIndexOrThrow(MediaStore.Images.ImageColumns.Data);
                cursor.MoveToFirst();
                fullPathToImage = cursor.GetString(colData);
            }
            return fullPathToImage;
        }
        catch (Exception ex)
        {
            Toast.MakeText(Xamarin.Forms.Forms.Context, "Unable to get path", ToastLength.Long).Show();
        }
        return null;
    }

2.在要打開選擇器的特定活動中調用以下內容

 public void OpenGallery()
    {
        try
        {
            var imageIntent = new Intent(Intent.ActionPick);
            imageIntent.SetType("image/*");
            imageIntent.PutExtra(Intent.ExtraAllowMultiple, true);
            imageIntent.SetAction(Intent.ActionGetContent);
            this.StartActivityForResult(Intent.CreateChooser(imageIntent, "Select photo"), OPENGALLERYCODE);
            Toast.MakeText(this, "Tap and hold to select multiple photos.", ToastLength.Short).Show();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            Toast.MakeText(this, "Error. Can not continue, try again.", ToastLength.Long).Show();
        }
    }


    void ClearFileDirectory()
    {
        var directory = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures), ImageHelpers.collectionName).Path.ToString();

        if (Directory.Exists(directory))
        {
            var list = Directory.GetFiles(directory, "*");
            if (list.Length > 0)
            {
                for (int i = 0; i < list.Length; i++)
                {
                    File.Delete(list[i]);
                }
            }
        }
    }


   //collectionName is the name of the folder in your Android Pictures directory.
    public static readonly string collectionName = "TmpPictures";

    public string SaveFile(byte[] imageByte, string fileName)
    {
        var fileDir = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures), collectionName);
        if (!fileDir.Exists())
        {
            fileDir.Mkdirs();
        }

        var file = new Java.IO.File(fileDir, fileName);
        System.IO.File.WriteAllBytes(file.Path, imageByte);

        return file.Path;
    }

    public string CompressImage(string path)
    {
        byte[] imageBytes;

        //Get the bitmap.
        var originalImage = BitmapFactory.DecodeFile(path);

        //Set imageSize and imageCompression parameters.
        var imageSize = .86;
        var imageCompression = 67;

        //Resize it and then compress it to Jpeg.
        var width = (originalImage.Width * imageSize);
        var height = (originalImage.Height * imageSize);
        var scaledImage = Bitmap.CreateScaledBitmap(originalImage, (int)width, (int)height, true);

        using (MemoryStream ms = new MemoryStream())
        {
            scaledImage.Compress(Bitmap.CompressFormat.Jpeg, imageCompression, ms);
            imageBytes = ms.ToArray();
        }

        originalImage.Recycle();
        originalImage.Dispose();
        GC.Collect();

        return SaveFile(imageBytes, Guid.NewGuid().ToString());
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM