是否可以检索imageView的位图?
我试过this但是我得到的是NullPointerException并且编辑后的代码在monodroid中不起作用
答案 0 :(得分:1)
您应该可以通过 Drawable 属性访问指定的图像。确保在访问之前检查null。
答案 1 :(得分:1)
这是一个使用它的工作示例。
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap save = view.getDrawingCache();
其中view
为ImageView
,如果您尝试保存它。这里是我的保存方法,从我的imageview获取视图并将其保存为.PNG
void Save() {
if (null != view.getDrawable()) {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
save = view.getDrawingCache();
final File myDir = new File(folder);
myDir.mkdirs();
final Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
final String fname = "StyleMe-" + n + ".png";
file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
final FileOutputStream out = new FileOutputStream(file);
save.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
Toast.makeText(getApplication(), "Image Saved",
Toast.LENGTH_SHORT).show();
} catch (final Exception e) {
Toast.makeText(getApplication(),
"Something Went Wrong check if you have Enough Memory",
Toast.LENGTH_LONG).show();
}
} else {
final Toast tst = Toast.makeText(getApplication(),
"Please Select An Image First", Toast.LENGTH_LONG);
tst.setGravity(Gravity.CENTER, 0, 0);
tst.show();
}
view.setDrawingCacheEnabled(false);
}
这是我的变量。
String folder = "/sdcard/Pictures/StyleMe";
static File file;
答案 2 :(得分:1)
感谢您的回答。最后我这样做了:
var imageView = FindViewById<ImageView>(Resource.Id.image);
Bitmap bmp = ((BitmapDrawable)imageView.Drawable).Bitmap;
ps:我有一个NullPointerException,因为imageView在另一个视图中。