我通过组合两个图像来创建位图。我现在尝试存储它,保存它,看到它并在应用程序中使用它。
这就是我创建新位图的方法:
string OnePath = "backgroundimage";
string TwoPath = "objectimage";
var OneImage = BitmapFactory.DecodeResource(Resources, Resources.GetIdentifier(OnePath, "drawable", PackageName));
var TwoImage = BitmapFactory.DecodeResource(Resources, Resources.GetIdentifier(TwoPath, "mipmap", PackageName));
Bitmap bitmap = OneImage.Copy(Bitmap.Config.Argb8888, true);
Canvas canvas = new Canvas(bitmap);
Rect baseRect = new Rect(0, 0, OneImage.Width, OneImage.Height);
Rect frontRect = new Rect(22, 31, 79, 79);
canvas.DrawBitmap(TwoImage, frontRect, baseRect, null);
所以我现在想要保存我创建的这个新位图。我试图做的是以下几点:
var documentsDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string jpgFilename = System.IO.Path.Combine(documentsDirectory, OnePath);
App.AppFileName = System.IO.Path.Combine(documentsDirectory, OnePath);
System.Diagnostics.Debug.WriteLine(App.AppFileName);
其中App.AppFileName
是存储图像路径的字符串。我现在不确定创建的图像是否实际保存在那里。如果我用连接到我的电脑的Android手机打开android文件管理器,我在App.AppFileName
路径中看不到该图像。
答案 0 :(得分:0)
因为图片是大文件,我建议您将其保存在外部存储空间 Here you can find what is external storage上,路径如:/storage/sdcard/Pictures
,我为您制作了一个演示:
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Util;
using Android.Graphics;
using System.IO;
namespace SaveBitmap
{
[Activity(Label = "SaveBitmap", MainLauncher = true)]
public class MainActivity : Activity
{
string bitmapPath;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
bitmapPath = getPath("bitmap.png");
Bitmap bitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.capture);
//save bitamp
saveBitmap(bitmap);
ImageView iv = FindViewById<ImageView>(Resource.Id.iv);
//load bitmap
Bitmap bi = BitmapFactory.DecodeFile(bitmapPath);
iv.SetImageBitmap(bi);
}
public string getPath(string fileName) {
var sdCardPath = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures);
Log.Error("lv", sdCardPath.AbsolutePath);
string filePath = System.IO.Path.Combine(sdCardPath.AbsolutePath, fileName);
return filePath;
}
public void saveBitmap(Bitmap bitmap ) {
FileStream stream = new FileStream(bitmapPath, FileMode.Create);
bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
stream.Close();
}
}
}
不要忘记在清单中添加这些:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
如果您在Android 6.0以上运行您的应用,则需要运行时权限。
我添加了运行时权限。
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Util;
using Android.Graphics;
using System.IO;
using System;
using Android;
using Android.Content.PM;
using Android.Runtime;
namespace SaveBitmap
{
[Activity(Label = "SaveBitmap", MainLauncher = true)]
public class MainActivity : Activity
{
string bitmapPath;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
bitmapPath = getPath("bitmap.png");
permissionCheck();
}
private void permissionCheck()
{
if (CheckSelfPermission(Manifest.Permission.ReadExternalStorage) != Permission.Granted)
{
// Should we show an explanation?
if (ShouldShowRequestPermissionRationale(Manifest.Permission.ReadExternalStorage))
{
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
}
else
{
// No explanation needed, we can request the permission.
RequestPermissions(new String[] { Manifest.Permission.ReadExternalStorage }, 1);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
else {
saveAndLoadBitmap();
}
}
public string getPath(string fileName) {
var sdCardPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures);
Log.Error("lv", sdCardPath.AbsolutePath);
string filePath = System.IO.Path.Combine(sdCardPath.AbsolutePath, fileName);
return filePath;
}
public void saveBitmap(Bitmap bitmap ) {
FileStream stream = new FileStream(bitmapPath, FileMode.Create);
bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
stream.Close();
}
public void saveAndLoadBitmap() {
Bitmap bitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.capture);
saveBitmap(bitmap);
ImageView iv = FindViewById<ImageView>(Resource.Id.iv);
Bitmap bi = BitmapFactory.DecodeFile(bitmapPath);
iv.SetImageBitmap(bi);
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
switch (requestCode)
{
case 1:
{
// If request is cancelled, the result arrays are empty.
if (grantResults.Length > 0 && grantResults[0] == Permission.Granted)
{
// permission was granted, yay! Do the
// contacts-related task you need to do.
saveAndLoadBitmap();
}
else
{
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
}
}