2016-10-19 61 views
0

在我的應用程序中,我從相機捕獲圖像,在棉花糖上它工作正常,但在較低的版本上,它會給隨機崩潰。有時它可以正常工作,有時不會。下面是代碼,我用我的應用程序在較低的Android版本上捕獲圖像的隨機崩潰

 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結果我這樣做:

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

壓縮圖像

 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; 

} 

請提示這裏有什麼問題。 我需要圖片路徑(壓縮和旋轉)。有時候它可以很好地工作,並且在其他時候可以保存圖像並返回到應用程序並重新啓動調用活動

+0

發佈您的日誌貓,所以有人可以識別錯誤而不浪費時間。 – Piyush

+0

我在onActiviyt結果中的圖像路徑上返回null。這是因爲應用程序重新啓動並且其路徑得到空值,因爲只有當我打開相機時才設置路徑 – Androiduser

回答

1

這是因爲應用程序被重新啓動,它的路徑得到空值,因爲當我打開相機

最可能的路徑只能置位,而這是在後臺你的進程被終止。 This is perfectly normal behavior。如果用戶旋轉屏幕,您也會有類似的症狀。

通過onSaveInstanceState()將您的路徑保存在保存的實例狀態Bundle中。從已保存的實例狀態Bundle中的onCreate()onRestoreInstanceState()恢復它。然後,當控制權通過onActivityResult()返回給您時,您將擁有自己的路徑。 This sample app演示該過程,具體用ACTION_IMAGE_CAPTURE

+0

謝謝,它解決了我的問題。 – Androiduser