无法从解码文件位图传递图像路径(Xamarin Android)路径返回Null异常错误

问题描述 投票:0回答:1

[我正在尝试使用C#在Xamarin Android中传递我的图像。在第一个活动中,我正在使用BitmapFactory.DecodeFile(uri_path);将图像路径解码为位图文件。我还将位图图片设置为imgView.SetImageBitmap(BitMap);,然后在按钮单击中传递该URI路径,该按钮将使用intent.PutExtra("path", mCurrentPhotoPath);将图片引用至下一个活动。在下一个活动中,我想从第一个活动中获取传递路径值。

string path = intent.GetStringExtra("path");

            if (path != null)
            {
                Bitmap bitmap = BitmapFactory.DecodeFile(path);
                m_imageView.SetImageBitmap(bitmap);
            }

            else
            {
                Toast.MakeText(this, "Your picture empty!", ToastLength.Long).Show();
            }

但是,我得到一个带有空异常错误的错误。路径返回null,程序崩溃。请帮我!几个小时后,我感到卡住了。有什么帮助吗?这是我的完整代码:

第一个活动-我的onActivityResult

private Button btnContinue, btnCapture;
        private ImageView imgView;
        public Bitmap BitMap;
        private Android.Net.Uri filePath;
        private const int c_TAKE_IMAGE_REQUSET = 72;
        private ProgressBar progressBar;
        public String mCurrentPhotoPath = null;

public override void OnActivityResult(int requestCode, int resultCode, Intent data) 
    {
        base.OnActivityResult(requestCode, resultCode, data);

    if (requestCode == c_TAKE_IMAGE_REQUSET &&
        resultCode == (int)Result.Ok &&
        data != null)
    {
        filePath = data.Data;
        //String mCurrentPhotoPath = null;
        try
        {
            mCurrentPhotoPath = GetRealPathFromURI(filePath);
        }
        catch (Exception ex)
        {
            // Failed for some reason.
        }

        try
        {
            BitMap = (Bitmap)data.Extras.Get("data");
            imgView.SetImageBitmap(BitMap);
            BitMap = BitmapFactory.DecodeFile(mCurrentPhotoPath);
            System.Diagnostics.Debug.WriteLine("Image path == " + mCurrentPhotoPath);
        }
        catch (System.IO.IOException ex)
        {
            System.Console.WriteLine(ex);
        }

        var bitmap = BitmapFactory.DecodeFile(mCurrentPhotoPath); // decode the path into bitmap 
        imgView.SetImageBitmap(bitmap);
    }
}

private string GetRealPathFromURI(Android.Net.Uri uri)
    {
        string doc_id = "";
        using (var c1 = this.View.Context.ContentResolver.Query(uri, null, null, null, null))
        {
            c1.MoveToFirst();
            string document_id = c1.GetString(0);
            doc_id = document_id.Substring(document_id.LastIndexOf(":") + 1);
        }

        string path = null;

        // The projection contains the columns we want to return in our query.
        string selection = Android.Provider.MediaStore.Images.Media.InterfaceConsts.Id + " =? ";
        using (var cursor = this.View.Context.ContentResolver.Query(Android.Provider.MediaStore.Images.Media.ExternalContentUri, null, selection, new string[] { doc_id }, null))
        {
            if (cursor == null) return path;
            var columnIndex = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data);
            cursor.MoveToFirst();
            path = cursor.GetString(columnIndex);
        }
        return path;
    }
}

第二项活动-onCreate受保护的重写void OnCreate(Bundle savedInstanceState){base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.Upload);

        string path = intent.GetStringExtra("path");

        if (path != null)
        {
            Bitmap bitmap = BitmapFactory.DecodeFile(path);
            m_imageView.SetImageBitmap(bitmap);
        }

        else
        {
            Toast.MakeText(this, "Your picture empty!", ToastLength.Long).Show();
        }

        m_upload.Click += M_upload_Click;

    }

编辑:我将我的放置字符串额外放入按钮中。该按钮是Button Continue,它是对下一个活动的引用

private void btnContinue_Click(object sender, EventArgs e)
        {
            Busy();
            Intent intent = new Intent(Activity, typeof(UploadActivity));
            intent.PutExtra("path", mCurrentPhotoPath);
            StartActivity(intent);
        }
c# android xamarin xamarin.android
1个回答
0
投票

我认为这只会在您拍照后发生,因为filePath = data.Data;返回null,如果您从照片中选择一张照片,它将返回其uri

所以您可以在拍照后尝试传递位图的byte[]数据:

public override void OnActivityResult(int requestCode, int resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);

        if (requestCode == c_TAKE_IMAGE_REQUSET &&
            resultCode == (int)Result.Ok &&
            data != null)
        {

            try
            {
                mBitMap = (Bitmap)data.Extras.Get("data");
                imgView.SetImageBitmap(mBitMap);
                using (var stream = new MemoryStream())
                {
                    mBitMap.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);
                    bitmapData = stream.ToArray();
                }
            }
            catch (System.IO.IOException ex)
            {
                System.Console.WriteLine(ex);
            }
        }
    }

然后您可以通过bitmapData

Intent intent = new Intent(Activity, typeof(UploadActivity));
intent.PutExtra("img", bitmapData);
StartActivity(intent);

在新活动中显示它:

byte[] bitmapbyte = Intent.GetByteArrayExtra("img");
Bitmap bitmap = BitmapFactory.DecodeByteArray(bitmapbyte, 0, bitmapbyte.Length);
m_imageView.SetImageBitmap(bitmap);
© www.soinside.com 2019 - 2024. All rights reserved.