写真を撮って、それをサーバーにアップロードする前にプレビューを表示する必要があるアンドロイドアプリで作業しています。 (簡単に聞こえますか?少なくとも私は考えました)。 私の問題は、時には画像を表示することができますが、ほとんどの時間は、アプリケーションが閉じているため、メモリ不足のエラーだと思います。ここでカメラインテントでメモリ不足エラーを処理するimageView preview
は意図を使用している方法です。ここで
imageCaptureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
stamp = accNum[pos] + "_" + s.format(new Date());
File f = new File(Environment.getExternalStorageDirectory(), stamp + ".jpg");
chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
imageToUploadUri = Uri.fromFile(f);
imagePath = f.getPath();
stopFusedLocation();
startActivityForResult(chooserIntent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
});
は、私がここで撮影された画像
private Bitmap getBitmap(String path) {
Uri uri = Uri.fromFile(new File(path));
InputStream in = null;
try {
final int IMAGE_MAX_SIZE = 1200000; // 1.2MP
in = getActivity().getContentResolver().openInputStream(uri);
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, o);
in.close();
int scale = 1;
while ((o.outWidth * o.outHeight) * (1/Math.pow(scale, 2)) >
IMAGE_MAX_SIZE) {
scale++;
}
Log.d("", "scale = " + scale + ", orig-width: " + o.outWidth + ", orig-height: " + o.outHeight);
Bitmap b = null;
in = getActivity().getContentResolver().openInputStream(uri);
if (scale > 1) {
scale--;
// scale to max possible inSampleSize that still yields an image
// larger than target
o = new BitmapFactory.Options();
o.inSampleSize = scale;
b = BitmapFactory.decodeStream(in, null, o);
// resize to desired dimensions
int height = b.getHeight();
int width = b.getWidth();
Log.d("", "1th scale operation dimenions - width: " + width + ", height: " + height);
double y = Math.sqrt(IMAGE_MAX_SIZE
/(((double) width)/height));
double x = (y/height) * width;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x,
(int) y, true);
b.recycle();
b = scaledBitmap;
System.gc();
} else {
b = BitmapFactory.decodeStream(in);
}
in.close();
Log.d("", "bitmap size - width: " + b.getWidth() + ", height: " +
b.getHeight());
return b;
} catch (IOException e) {
Log.e("", e.getMessage(), e);
return null;
}
}
をデコードする方法である私は
/**
* Receiving activity result method will be called after closing the camera
* */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
if(imageToUploadUri != null){
Uri selectedImage = imageToUploadUri;
getActivity().getContentResolver().notifyChange(selectedImage, null);
Bitmap reducedSizeBitmap = getBitmap(imageToUploadUri.getPath());
if(reducedSizeBitmap != null){
bkl.setImageBitmap(reducedSizeBitmap);
}else{
Toast.makeText(getContext(),"Error while capturing Image",Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(getActivity(),"Error while capturing Image",Toast.LENGTH_LONG).show();
}
}
}
してくださいドン活動の結果をどのように処理するかされます私はこれに関する他のすべての質問を読んだので、重複した質問はしない働く私はandroid:largeHeap = true
をマニフェストで試してみて、インスタンスの状態を復元しても、何も動作しないようです。
助けてください。
lol #spaghetti_code。大丈夫は解決しましたが、これらの行は決して使用されません。 – vinstar