我正在尝试制作一个浏览按钮,供用户选择图像。我使用以下代码:
void BrowseButton_Click(object sender, EventArgs e)
{
Intent intent = new Intent(); intent.SetType("image/*");
intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(intent, "Select Picture"), 1);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Android.Content.Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode == Result.Ok && requestCode == 1)
{
ImageView imageView = FindViewById<ImageView>(Resource.Id.MainimageView);
Android.Net.Uri selectedImageUri = data.Data;
var bitmap = MediaStore.Images.Media.GetBitmap(ContentResolver, data.Data);
SherarClass.Uri = getRealPathFromURI(data.Data);
BitmapDrawable dd = new BitmapDrawable(bitmap);
imageView.SetImageDrawable(dd);
bitmap.Dispose();
}
}
在第一次和第二次之后它会抛出一个Java.Long.Error异常。
堆栈跟踪:
at Android.Runtime.JNIEnv.CallStaticObjectMethod (IntPtr jclass, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00000] in <filename unknown>:0
at Android.Provider.MediaStore+Images+Media.GetBitmap (Android.Content.ContentResolver cr, Android.Net.Uri url) [0x00000] in <filename unknown>:0
at Scannet.Activity1.OnActivityResult (Int32 requestCode, Result resultCode, Android.Content.Intent data) [0x00000] in <filename unknown>:0
--- End of managed exception stack trace ---
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:556)
at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:706)
at scannet.Activity1.n_onActivityResult(Native Method)
at scannet.Activity1.onActivityResult(Activity1.java:37)
at android.app.Activity.dispatchActivityResult(Activity.java:4649)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2996)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3043)
at android.app.ActivityThread.access$1100(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1188)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
void BrowseButton_Click(object sender, EventArgs e)
{
Intent intent = new Intent(); intent.SetType("image/*");
intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(intent, "Select Picture"), 1);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Android.Content.Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode == Result.Ok && requestCode == 1)
{
ImageView imageView = FindViewById<ImageView>(Resource.Id.MainimageView);
Android.Net.Uri selectedImageUri = data.Data;
BitmapFactory.Options op = new BitmapFactory.Options();
op.InSampleSize = 2;
SherarClass.Uri = getRealPathFromURI(data.Data);
using (Bitmap bitmap = BitmapFactory.DecodeFile(SherarClass.Uri, op))
{
imageView.SetImageBitmap(bitmap);
GC.Collect();
}
}
}
public String getRealPathFromURI(Android.Net.Uri contentUri)
{
String[] projection = new String[] {Android.Provider.MediaStore.MediaColumnsConsts.Data };
ContentResolver cr = this.ContentResolver;
Android.Database.ICursor cursor = cr.Query(contentUri, projection,null, null, null);
if (cursor != null && cursor.Count > 0)
{
cursor.MoveToFirst();
int index =cursor.GetColumnIndex(Android.Provider.MediaStore.MediaColumnsConsts.Data);
return cursor.GetString(index);
}
return null;
}
答案 1 :(得分:0)
尝试使用此代码。 getPic是一个按钮,用户必须单击该按钮才能打开MediaStore以查看图库类型查看器中的所有图像。
getPic.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent it = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(it, 101);
}
});
然后在onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 101:
if (resultCode == RESULT_OK) {
Uri photoUri = data.getData();
try {
Bitmap currentImage = MediaStore.Images.Media.getBitmap
(this.getContentResolver(), photoUri);
imgView.setImageBitmap(currentImage);
}
这段代码可以使用,因为我在个人项目中使用过它。如果出现问题,请告诉我。
答案 2 :(得分:0)
我会尝试使用以下活动结果:
protected override void OnActivityResult(int requestCode, Result resultCode, Android.Content.Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode == Result.Ok && requestCode == 1)
{
ImageView imageView = FindViewById<ImageView>(Resource.Id.MainimageView);
Android.Net.Uri selectedImageUri = data.Data;
using (var bitmap = MediaStore.Images.Media.GetBitmap(ContentResolver, selectedImageUri))
{
SherarClass.Uri = getRealPathFromURI(selectedImageUri);
using (var dd = new BitmapDrawable(bitmap))
imageView.SetImageDrawable(dd);
}
}
}
当Bitmap
和BitmapDrawable
不再需要时,它会同时处理它,然后不需要自己处理它。