2016-10-19 8 views
0

私のアプリケーションでは、カメラから画像をキャプチャしていますが、marshmallowでは正常に動作しますが、下位バージョンではランダムクラッシュが発生します。場合によってはうまく動作することもあります。画像下位のアンドロイドバージョンで画像をキャプチャする際にランダムクラッシュが発生する

 public static String compressImage(Context context, Uri imageUri) { 
    String filePath = getRealPathFromURI(context, imageUri); 

    Bitmap scaledBitmap = null; 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    Bitmap bmp = BitmapFactory.decodeFile(filePath,options); 

    int actualHeight = options.outHeight;//2988 
    int actualWidth = options.outWidth; //5312 

    float maxHeight = 1200.0f; 
    float maxWidth = 1200.0f; 
    /*float maxHeight = 816.0f; 
    float maxWidth = 612.0f;*/ 
    float imgRatio = actualWidth/actualHeight; 
    float maxRatio = maxWidth/maxHeight; 

    if (actualHeight > maxHeight || actualWidth > maxWidth) { 
     if (imgRatio < maxRatio) { 
      imgRatio = maxHeight/actualHeight; 
      actualWidth = (int) (imgRatio * actualWidth); 
      actualHeight = (int) maxHeight; 
     } else if (imgRatio > maxRatio) { 
      imgRatio = maxWidth/actualWidth; 
      actualHeight = (int) (imgRatio * actualHeight); 
      actualWidth = (int) maxWidth; 
     } else { 
      actualHeight = (int) maxHeight; 
      actualWidth = (int) maxWidth; 

     } 
    } 

    options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight); 
    options.inJustDecodeBounds = false; 
    options.inDither = false; 
    options.inPurgeable = true; 
    options.inInputShareable = true; 
    options.inTempStorage = new byte[16*1024]; 

    try{ 
     bmp = BitmapFactory.decodeFile(filePath,options); 
    } 
    catch(OutOfMemoryError exception){ 
     exception.printStackTrace(); 

    } 
    try{ 
     scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.ARGB_8888); 
    } 
    catch(OutOfMemoryError exception){ 
     exception.printStackTrace(); 
    } 

    float ratioX = actualWidth/(float) options.outWidth; 
    float ratioY = actualHeight/(float)options.outHeight; 
    float middleX = actualWidth/2.0f; 
    float middleY = actualHeight/2.0f; 

    Matrix scaleMatrix = new Matrix(); 
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY); 

    Canvas canvas = new Canvas(scaledBitmap); 
    canvas.setMatrix(scaleMatrix); 
    canvas.drawBitmap(bmp, middleX - bmp.getWidth()/2, middleY - bmp.getHeight()/2, new Paint(Paint.FILTER_BITMAP_FLAG)); 


    ExifInterface exif; 
    try { 
     exif = new ExifInterface(filePath); 

     int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0); 
     Log.d("EXIF", "Exif: " + orientation); 
     Matrix matrix = new Matrix(); 
     if (orientation == 6 || orientation == 0) { 
      matrix.postRotate(90); 
      Log.d("EXIF", "Exif: " + orientation); 
     } else if (orientation == 3) { 
      matrix.postRotate(180); 
      Log.d("EXIF", "Exif: " + orientation); 
     } else if (orientation == 8) { 
      matrix.postRotate(270); 
      Log.d("EXIF", "Exif: " + orientation); 
     } 
     scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    FileOutputStream out = null; 
    String filename = getFilename(); 
    try { 
     out = new FileOutputStream(filename); 
     scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

    return filename; 

} 

を圧縮するための

Uri imagePathUri = Uri.parse(SingleTon.getInstance().imageFile.getPath()); 
     String picturePath = compressImage(context, imagePathUri); 

親切に、ここで間違っているものをお勧め:ここに私のアプリ

 String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     String photoName = "Image_" + timeStamp + ".jpg"; 
     String imageFile = new File(extStorageDirectory, photoName); 
     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     // Ensure that there's a camera activity to handle the intent 
     if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) { 
      // Create the File where the photo should go 
      try { 
       SingleTon.getInstance().imageFile.createNewFile(); 
      } catch (IOException ex) { 
       // Error occurred while creating the File 
       ex.printStackTrace(); 
      } 

      // Continue only if the File was successfully created 
      if (SingleTon.getInstance().imageFile != null) { 
       Log.e("Check1 ", SingleTon.getInstance().imageFile+";"); 
       takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(SingleTon.getInstance().imageFile)); 
       ((Activity)context).startActivityForResult(takePictureIntent, Constant.CAMERA_PIC_REQUEST); 
      } 
     } 

onActivity結果に私はこれをやっている私が使用していたコードです。 私は画像パス(圧縮と回転)が必要です。ときどきそれは完璧にうまく動作し、イメージを保存するときにはアプリに戻り、アクティビティを再起動します。

+0

あなたのログcatを投稿すると、時間を無駄にせずに誰かがエラーを特定できるようになります。 – Piyush

+0

私はonActiviyt結果の画像パスにnullを返します。これは、アプリケーションが再起動され、パスがヌル値になるためです。パスがカメラを開いたときにのみ設定されるからです。 – Androiduser

答えて

1

アプリを再起動しますので、これがあると、そのパスがnull値を取得し、パスが設定されているので、私はほとんどのカメラ

を開いた場合にのみ、それがバックグラウンドで動作している一方で、あなたのプロセスが終了されています。 This is perfectly normal behavior。また、ユーザーが画面を回転させると、同様の現象が発生します。

保存されたインスタンス状態BundleでパスをonSaveInstanceState()経由で保存します。保存したインスタンスの状態BundleonCreate()またはonRestoreInstanceState()に復元します。その後、制御がonActivityResult()を経由してあなたに戻ったときにあなたのパスを取得します。 This sample appは、具体的にはACTION_IMAGE_CAPTUREでプロセスを示しています。

+0

ありがとう、私の問題を解決しました。 – Androiduser

関連する問題