ここで私は(それがMonoDroid/C#であります申し訳ありませんが、それは、Android/Javaで動作するように光の変更が必要になります)ImageViewの内に表示するビットマップのを取得するために使用されるコードは次のとおりです。
using System.IO;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Widget;
namespace MyApp.Util
{
public static class BitmapHelpers
{
/// <summary>
/// This method will recyle the memory help by a bitmap in an ImageView
/// </summary>
/// <param name="imageView">Image view.</param>
public static void RecycleBitmap(this ImageView imageView)
{
if (imageView == null) {
return;
}
Drawable toRecycle = imageView.Drawable;
if (toRecycle != null) {
((BitmapDrawable)toRecycle).Bitmap.Recycle();
}
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{
// Raw height and width of image
int height = options.OutHeight;
int width = options.OutWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth)
{
int halfHeight = height/2;
int halfWidth = width/2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight/inSampleSize) > reqHeight
&& (halfWidth/inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
/// <summary>
/// Load the image from the device, and resize it to the specified dimensions.
/// </summary>
/// <returns>The and resize bitmap.</returns>
/// <param name="fileName">File name.</param>
/// <param name="width">Width.</param>
/// <param name="height">Height.</param>
public static Bitmap LoadAndResizeBitmap(this string fileName, int width, int height)
{
// First we get the the dimensions of the file on disk
BitmapFactory.Options options = new BitmapFactory.Options
{
InPurgeable = true,
InJustDecodeBounds = true
};
BitmapFactory.DecodeFile(fileName, options);
options.InSampleSize = calculateInSampleSize(options, width, height);
options.InJustDecodeBounds = false;
Bitmap resizedBitmap = BitmapFactory.DecodeFile(fileName, options);
return resizedBitmap;
}
}
}
使用例
public static Bitmap LoadBitmap(File imageFile, int w, int h)
{
Bitmap bitmap = null;
if (imageFile != null)
{
if((new File(imageFile.Path)).Exists())
{
bitmap = imageFile.Path.LoadAndResizeBitmap(w, h);
}
}
return bitmap;
}
OnActivityResultスニペット(ギャラリーに写真を使用可能に)
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
switch ((ActivityRequestCode)requestCode)
{
case ActivityRequestCode.Camera:
// make it available in the gallery
if (_imageFile != null)
{
Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
Uri contentUri = Uri.FromFile(_imageFile);
mediaScanIntent.SetData(contentUri);
SendBroadcast(mediaScanIntent);
_pictureRequestingFragment.PictureReady(_imageFile);
}
break;
case ActivityRequestCode.Map:
break;
}
}
あなたはマニフェストファイルに権限を追加しましたか? –
[下のリンクで私の答えをチェックアウト](http://stackoverflow.com/questions/43450296/how-to-get-save-an-image-at-location-and-retrieve-it-for-imageview-in- android-ap/43450770#43450770) –
ビットマップが大きすぎる可能性があり、デバイスにエンボスメモリがない可能性がありますか? BitmapToLargeExceptionのログを確認してください。 –