我正在使用Xamarin.Android中的一些图像,我在下面用来打开文件
Intent = new Intent();
Intent.SetType("image/*");
Intent.PutExtra(Intent.ExtraAllowMultiple,true);
Intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(Intent, "Select Picture"), code);
我只是使用Bitmap.CompressFormat.Jpeg, 90, fs);
压缩文件,其中fs是一个Stream来保存文件。我想覆盖打开的uri(比如用现有的位图替换现有的位图(bmp)。
我想批量做这件事。
我怎样才能做到这一点?
答案 0 :(得分:0)
试试这个:
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Content;
using Android.Runtime;
using Java.Lang;
using Android.Net;
using System.Collections.Generic;
using Android.Graphics;
using System.IO;
namespace PictureCopr
{
[Activity(Label = "PictureCopr", MainLauncher = true)]
public class MainActivity : Activity
{
ImageView miv;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
miv = FindViewById<ImageView>(Resource.Id.iv);
Intent intent = new Intent();
intent.SetType("image/*");
intent.PutExtra(Intent.ExtraAllowMultiple, true);
intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(intent, "Select Picture"), 1);
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
if (resultCode == Result.Ok)
{
ClipData clipData = data.ClipData;
Uri uri = data.Data;
if (null == uri)//multi
{
List<Uri> list = new List<Uri>(clipData.ItemCount);
for (int i = 0; i < clipData.ItemCount; i++)
{
ClipData.Item itemAt = clipData.GetItemAt(i);
list.Add(itemAt.Uri);
Android.Util.Log.Error("uri", itemAt.Uri.ToString());
try
{
Bitmap bitmap = BitmapFactory.DecodeStream(ContentResolver.OpenInputStream(itemAt.Uri));
var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
var filePath = System.IO.Path.Combine(sdCardPath, i+"test.png");
var stream = new FileStream(filePath, FileMode.Create);
bitmap.Compress(Bitmap.CompressFormat.Png, 90, stream);
}
catch (Exception e)
{
Android.Util.Log.Error("Exception", e.Message);
}
}
}
else
{// single
try
{
Bitmap bitmap = BitmapFactory.DecodeStream(ContentResolver.OpenInputStream(uri));
var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
var filePath = System.IO.Path.Combine(sdCardPath, "test.png");
Android.Util.Log.Error("lv====", filePath);
var stream = new FileStream(filePath, FileMode.Create);
bitmap.Compress(Bitmap.CompressFormat.Png, 90, stream);
stream.Flush();
stream.Close();
}
catch (Exception e)
{
Android.Util.Log.Error("Exception", e.Message);
}
}
}
else
{
Android.Util.Log.Error("MainActivtiy", "operation error");
}
base.OnActivityResult(requestCode, resultCode, data);
}
}
}
替换它:
var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
var filePath = System.IO.Path.Combine(sdCardPath, i+"test.png");
用这个:
var sdCardPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures);
var filePath = System.IO.Path.Combine(sdCardPath.AbsolutePath, i+"test.png");
写入Sd卡演示:
using Android.App;
using Android.Widget;
using Android.OS;
using System.IO;
using Java.IO;
namespace WritToSd
{
[Activity(Label = "WritToSd", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
if (IsSdCardExist())
{
Java.IO.File sdCardPath = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures);
// destPath: /storage/emulated/0/Pictures/test.png
string destPath = Path.Combine(sdCardPath.AbsolutePath, "test.png");
string originPath = Path.Combine(sdCardPath.AbsolutePath, "nULSa.png");
Android.Util.Log.Error("lv", destPath);
FileOutputStream fos = new FileOutputStream(destPath , false);
FileInputStream fis = new FileInputStream(originPath);
int b;
byte[] d = new byte[1024 * 1024];
while ((b = fis.Read(d)) != -1)
{
fos.Write(d, 0, b);
}
fos.Flush();
fos.Close();
fis.Close();
}
else {
//do something
}
}
public bool IsSdCardExist()
{
return Environment.ExternalStorageState.Equals(
Environment.MediaMounted);
}
}
}