xamarin 拍照功能和相册浏览功能
根据路径获取图片失败 Bitmap resizedBitmap = BitmapFactory.DecodeFile( fileName, options);
SetContentView(Resource.Layout.jx_main);
// Create your application here
//其他安卓芯片的默认拍照路径
originalFile = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(
Android.OS.Environment.DirectoryPictures
), "zcb_pic_" + SystemClock.CurrentThreadTimeMillis() + ".jpg");
//高通芯片的默认拍照路径
finalFile = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(
Android.OS.Environment.DirectoryDcim + @"/Camera"
), "zcb_pic_" + SystemClock.CurrentThreadTimeMillis() + ".jpg");
//拍照路径
public static Bitmap LoadAndResizeBitmap(this string fileName, int width, int height)
{
// First we get the the dimensions of the file on disk
BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true };
BitmapFactory.DecodeFile(fileName, options);
// Next we calculate the ratio that we need to resize the image by
// in order to fit the requested dimensions.
int outHeight = options.OutHeight;
int outWidth = options.OutWidth;
int inSampleSize = 1;
if (outHeight > height || outWidth > width)
{
inSampleSize = outWidth > outHeight
? outHeight / height
: outWidth / width;
}
// Now we will load the image and have BitmapFactory resize it for us.
options.InSampleSize = inSampleSize;
options.InJustDecodeBounds = false;
Bitmap resizedBitmap = BitmapFactory.DecodeFile( fileName, options);
//Bitmap resizedBitmap = BitmapFactory.DecodeFile("file://" + fileName, options);
return resizedBitmap;
}
Bitmap resizedBitmap = BitmapFactory.DecodeFile( fileName, options);
如何修改改进
查考资料
1、权限问题
2、路径问题
你传的fileName是绝对路径?还是uri转的字符串呢?
好歹把错误信息也弄出来....
路径没错的话,主要也就权限问题,android11开始对文件存储做了划分,只能拿应用私有目录和系统公共目录的文件,而且不用申请权限,但11以下就要去申请读写权限了