【问题标题】:image rotation not working on Samsung Galaxy Nexus android图像旋转不适用于三星 Galaxy Nexus android
【发布时间】:2012-12-11 06:57:39
【问题描述】:

我已经在大约 25 台设备上测试了这段代码 sn-p,除了我现在尝试测试的三星 Galaxy Nexus 之外,它在所有设备上都运行良好。

这是方法,我很抱歉没有将其修剪以找到引发异常的确切位置,但 eclipse 的调试是 doodoo。

private void setupImageView() {
    imageLocation = currentPhotoPath;


    // Get the dimensions of the View
    Display display = getWindowManager().getDefaultDisplay();
    Point size = getDisplaySize(display);
    int targetW = size.x;

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;

    BitmapFactory.decodeFile(imageLocation, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW / targetW, photoH / targetW);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(imageLocation, bmOptions);
    //int rotationForImage = getRotationForImage(imageLocation);
    int rotationForImage = (whichCamera == 0 ? 90 : 270);
    if (rotationForImage != 0) {
        int targetWidth = rotationForImage == 90 || rotationForImage == 270 ? bitmap.getHeight() : bitmap.getWidth();
        int targetHeight = rotationForImage == 90 || rotationForImage == 270 ? bitmap.getWidth() : bitmap.getHeight();
        Bitmap rotatedBitmap = Bitmap.createBitmap(targetWidth, targetHeight, bitmap.getConfig());
        Canvas canvas = new Canvas(rotatedBitmap);
        Matrix matrix = new Matrix();
        matrix.setRotate(rotationForImage, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
        canvas.drawBitmap(bitmap, matrix, new Paint());

        bitmap.recycle();
        bitmap = rotatedBitmap;
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
        try
        {
                File f = new File(imageLocation);
                f.createNewFile();
                //write the bytes in file
                FileOutputStream fo = new FileOutputStream(f);
                fo.write(bytes.toByteArray());
                fo.close();
        }
        catch(java.io.IOException e){}
    }
    imageView.setImageBitmap(bitmap);
}

任何人都知道三星对关系的不同之处会导致引发异常吗?它在 Galaxy S III 上运行良好

【问题讨论】:

  • 讨厌问显而易见的问题......有什么例外?
  • 在我的 onActivityResult() 中抛出异常,当意图关闭并返回到上一个活动时,它会调用上述粘贴的方法。我通过手动将图像的旋转设置为 0 将其进一步缩小到旋转位。在这种情况下,if 块中的任何内容都不会被调用。这是例外情况:12-10 15:08:14.452: E/AndroidRuntime(9817): java.lang.RuntimeException: 传递结果失败 ResultInfo{who=null, request=904, result=-1, data=Intent { (有extras) }} 到活动 {com.myapp}: java.lang.NullPointerException
  • 看起来你提到的 if 块中的某些东西正在抛出 NPE - 这是真正的错误。不要担心 Activity/ResultInfo 的东西,它是下游的,由 NPE 触发。逐行查找空引用。
  • 看起来位图对象上的任何方法都导致它呕吐。即使我注释掉除此行之外的所有内容: int targetWidth = rotationForImage == 90 ||旋转图像 == 270 ? bitmap.getHeight() : bitmap.getWidth();它引发异常。我无法在 Eclipse 上设置断点,因此我再次为无法提供更好的信息而道歉。但我已将其范围缩小为至少在位图对象上调用的方法......这适用于除此设备之外的所有其他设备(无论如何我已经找到了)。
  • 好吧,事实证明我的相机意图没有将路径字符串传递给我的调用活动,所以你是对的,这是一个空指针异常。如果您想将其写为答案,我会将其标记为答案。非常感谢。有什么提示可以更好地使用 eclipse 进行调试吗?

标签: android android-image samsung-mobile


【解决方案1】:

看起来你提到的 if 块中的某些东西正在抛出 NPE - 这是真正的错误。不要担心 Activity/ResultInfo 的东西,它是下游的,由 NPE 触发。逐行查找空引用:-)

关于 Eclipse - 遗憾的是我没有太多经验。对于 Android,我个人使用 IntelliJ,调试效果很好。您是否能够调试其他 Java 代码(甚至是简单的 Hello, World)?

【讨论】:

  • 使用 Eclipse 的日志记录相当不错,尤其是 Log.d() 事件。我只是习惯于 Visual Studio,在那里我可以在断点处折腾,实际上让进程在该断点处停止,这样我就可以看到属性值等。如果 IntelliJ 在实际设备(而不是模拟器)上调试时允许这样做,说出这个词,我就会做出改变。
  • 我可以确认我已经使用 IntelliJ 在实际设备上获得断点 + 检查调试 :-)
猜你喜欢
  • 1970-01-01
  • 2012-03-19
  • 2019-02-10
  • 2013-02-21
  • 2017-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-03
相关资源
最近更新 更多