android旋转图像仅在某些手机上崩溃

标签 android image crash rotation

我有一个允许用户拍照的安卓应用程序。在这种情况下,图像并不总是正确显示,这取决于手机在图像捕获期间的方向。我提供了一个旋转图像功能,适用于大多数手机,但不是所有手机。事实上,当它不起作用时,应用程序就会崩溃。下面提供了旋转功能。我很感激任何可以使它在所有手机上运行的反馈。此外,该应用程序只是崩溃并且没有调用错误陷阱。

private void rotateImage(float degrees){

     try{

         String imageFile = Environment.getExternalStorageDirectory()+"/"+Imagefile;
         Bitmap bitmap = BitmapFactory.decodeFile(imageFile);                       
         int width = bitmap.getWidth();
         int height = bitmap.getHeight();

         // resize the bit map
         //matrix.postScale(scaleWidth, scaleHeight);

         // rotate the Bitmap
         Matrix matrix = new Matrix();
         matrix.postRotate(degrees);             
         // create the new Bitmap                         
         Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);                 

         imageAsset.setImageBitmap(rotatedBitmap);

         // clean up 
         bitmap = null;
         rotatedBitmap = null;
         matrix = null;

     }catch(Exception e){
        utility.logError(this,"{"+CLASS_NAME+"}[rotateImage] Error:  "+e.getMessage());         
     }
 }

最佳答案

可能是内存错误。可用堆取决于电话的类型。即使位图存储在 native 内存中(这使得它们的内存消耗更难跟踪),它们也被限制为相同的堆大小。大位图(来自 5MP)相机很容易耗尽应用程序中的所有内存。
不过,您必须发布错误日志才能确定。

对于初学者:
- 使用较小的图像

  • 清理位图使用 bitmap.recycle();

  • 编辑:
    好的,考虑一下我很确定这是内存不足。这就是错误没有被捕获的原因,因为它发生在 native 代码中。

    这是你做的:

    当您解码图像时,将其重新采样为较小的尺寸:
    BitmapFactory.Options resample = new BitmapFactory.Options();
    resample.inSampleSize = 4;  // whatever number seems apropriate 4 means 1/4 of the original
    bitmap = BitmapFactory.decodeFile(imageFile, resample);
    

    关于android旋转图像仅在某些手机上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7492022/

    相关文章:

    python - 使用 sess.run() 时 Tensorflow 崩溃

    java - 如何找到已弃用方法的替代品?

    java - Android X 上的 DelayedConfirmationView

    android - 在android中将图像和视频发送到服务器

    c# - FileNotFound 当我使用 Image.FromFile()

    android - Games.Players.getCurrentPlayer崩溃

    java - 在 Android Studio 中获取 json 数据的问题

    android - 在 xmpp openfire 中忘记密码

    javascript - 将路径 ('../mywebsite/images/$newname' ) 保存到数据库中

    在 IDLE 中运行模块时,Python-Pygame 总是崩溃且无响应