2 回答

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊
我寫(xiě)了一個(gè)關(guān)于它的演示。
我將圖片放在資源文件夾中,然后將其讀取為位圖
btnSelectImage = (Button)FindViewById(Resource.Id.btnSelectImage);
Bitmap bitmap = BitmapFactory.DecodeResource(this.ApplicationContext.Resources , Resource.Drawable.splashlogo);
btnSelectImage.Click += (o, e) =>
{
saveImageToGally(bitmap, this);
};
然后將其發(fā)送到圖庫(kù)中的特定相冊(cè)。
有代碼。
public void saveImageToGally(Bitmap finalBitmap,Context context)
{
//create a directory called MyCamera
string root = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDcim).ToString() + "/MyCamera/";
//create the Directory
System.IO.Directory.CreateDirectory(root);
File myDir = new File(root);
myDir.Mkdir();
//Image name
string fname = "Image-" + "Image1" + ".png";
File file = new File(myDir, fname);
Log.Error("FilePath", file.AbsolutePath);
if (file.Exists()) file.Delete();
Log.Error("FilePath", root + fname);
//total path
string path = root + fname;
try
{
var fs = new System.IO.FileStream(path, System.IO.FileMode.OpenOrCreate);
if (fs != null)
{
finalBitmap.Compress(Bitmap.CompressFormat.Png, 90, fs);
fs.Close();
}
}
catch (Exception e)
{
e.PrintStackTrace();
}
MediaScannerConnection.ScanFile(context, new string[] { file.Path }, new string[] { "image/jpeg" }, null);
}
請(qǐng)不要忘記添加以下權(quán)限。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- 2 回答
- 0 關(guān)注
- 126 瀏覽
添加回答
舉報(bào)