我正在尝试在手机中保存位图图像,但无法做到。
此代码在主线程中正常工作,但在插入AsyncTask
后它无效。
未处理的异常:System.UnauthorizedAccessException:访问 path /storage/emulated/0/Pictures/ATGImage.jpg“被拒绝。
using (var os = new FileStream(newfile, FileMode.CreateNew))<--here i am getting Error
{
image.Compress(Bitmap.CompressFormat.Jpeg, 90, os);
}
`
这是在AsyncTask中使用的代码
private class generateBarcode : AsyncTask<string,Java.Lang.Object,Bitmap>
{
generateActivity generate_activity;
private Bitmap bmp_image;`
public generateBarcode(generateActivity activity)
{
generate_activity = activity;
}
protected override void OnPreExecute()
{
generate_activity.progress_bar = new ProgressDialog(generate_activity);
generate_activity.progress_bar.SetTitle("wait !");
generate_activity.progress_bar.SetMessage("Generating image");
generate_activity.progress_bar.Show();
}
protected override Bitmap RunInBackground(params string[] @params)
{
try
{
long asset_number = Int64.Parse(@params[0]);
Debug("Thread _ Barcode", "asset_number " + asset_number.ToString());
BitMatrix matrix = new UPCAWriter().encode(asset_number.ToString(), ZXing.BarcodeFormat.UPC_A, 500, 300, null);
int width = matrix.Width;
int height = matrix.Height;
Bitmap image = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
image.SetPixel(x, y, (matrix[x, y] ? Android.Graphics.Color.Black : Android.Graphics.Color.White));
}
}
var path = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/" + Android.OS.Environment.DirectoryPictures, "ATGImage.jpg");
var newfile = new Java.IO.File(path);
if (newfile.Exists())
{
newfile.Delete();
using (var os = new FileStream(newfile, FileMode.CreateNew)) <--here i am getting Error
{
image.Compress(Bitmap.CompressFormat.Jpeg, 90, os);
}
}
else
{
using (var os = new FileStream(newfile, FileMode.CreateNew)) <--here i am getting Error
{
image.Compress(Bitmap.CompressFormat.Jpeg, 90, os);
}
}
Log.Debug("Thread _ Barcode_created", "asset_number " + asset_number.ToString());
return image;
}
catch (ThreadAbortException ex)
{
Log.Debug("Thread Aborted: ", ex.Message);
return null;
}
catch(ArgumentException ex)
{
Log.Debug("Barcode number ", ex.Message);
return null;
}
}
protected override void OnPostExecute(Bitmap result)
{
base.OnPostExecute(result);
generate_activity.bmp = result;
generate_activity.progress_bar.Dismiss();
}
}
这是Manifest.xml
个文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" android:versionCode="1" android:versionName="1.0" package="atg.droid.com">
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="24" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application android:label="ATG" android:icon="@drawable/Icon" android:theme="@android:style/Theme.Material.Light.DarkActionBar"></application>
</manifest>
`