2016-09-09 7 views
0

写真を撮って、それをサーバーにアップロードする前にプレビューを表示する必要があるアンドロイドアプリで作業しています。 (簡単に聞こえますか?少なくとも私は考えました)。 私の問題は、時には画像を表示することができますが、ほとんどの時間は、アプリケーションが閉じているため、メモリ不足のエラーだと思います。ここでカメラインテントでメモリ不足エラーを処理する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をマニフェストで試してみて、インスタンスの状態を復元しても、何も動作しないようです。

助けてください。

答えて

0

LeakCanaryを確認して、どこにアクティビティに関するリークがあるかを確認してください。

またuがスパゲッティコードのように持っている:

}else{ 
      Toast.makeText(getContext(),"Error while capturing Image",Toast.LENGTH_LONG).show(); 
     } 
    }else{ 
     Toast.makeText(getActivity(),"Error while capturing Image",Toast.LENGTH_LONG).show(); 

getAplication(); getContext(); 

これは方法はありません。コンテキスト、アクティビティを作成してすべてを修復します。

+0

lol #spaghetti_code。大丈夫は解決しましたが、これらの行は決して使用されません。 – vinstar

0

imageToUploadUriを渡すと、あなたが(Bitmap) intent.getExtras().get("data")を使用してintentのエキストラにサムネイルを取得することができますintentに合格することが重要ではない場合。それ以外の場合は、imageToUploadUri.getPath()が正しいパスを返すことを確認してください。以下のメソッドを使用して、uriからその画像のパスを取得できます。

public String getPathFromURI(Uri uri){ 
    String filePath = ""; 
    String[] filePahColumn = {MediaStore.Images.Media.DATA}; 
    Cursor cursor = getContentResolver().query(uri, filePahColumn, null, null, null); 
    if (cursor != null) { 
     if(cursor.moveToFirst()){ 
      int columnIndex = cursor.getColumnIndex(filePahColumn[0]); 
      filePath = cursor.getString(columnIndex); 
     } 
     cursor.close(); 
    } 
    return filePath; 
} 

これが役立ちます。