Search code examples
androidandroid-imagesamsung-mobile

image rotation not working on Samsung Galaxy Nexus android


I've tested this code snippet on about 25 devices and it works great on all of them except a Samsung Galaxy Nexus that I'm trying to test with now.

Here is the method and I apologize for not trimming it down to find the exact spot that's throwing the exception, but eclipse's debugging is 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);
}

anyone know what Samsung does differently with the nexus that would cause this to throw an exception? It works fine on a Galaxy S III


Solution

  • It looks like something in the if block you mention is throwing an NPE - that's the real bug here. Don't worry about the Activity/ResultInfo stuff, that is downstream and triggered by the NPE. Go line by line and look for the null reference :-)

    Regarding Eclipse - sadly I don't have much experience there. For Android I personally use IntelliJ and the debugging works well. Are you able to debug other Java code (even a simple Hello, World)?