ImageView setImageBitmap does not work on specific devices

I trained with the help Camera APIfor which I did the following:

a. Set the directory for the captured image (for startActivityForResult)

b. Set the bitmap so that the image can be displayed after saving in the application itself.

Here is the code for the following:

Directory setup.

private static File getOutputMediaFile(int type) {

    // External sdcard location
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            IMAGE_DIRECTORY_NAME);
    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                    + IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}

Global variables in the application

// Activity request codes
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;

// directory name to store the captured images
private static final String IMAGE_DIRECTORY_NAME = "my_camera_app";

private Uri fileUri;

// Views
ImageView photo;
Button camera;

Camera implementation logic

// Use camera function
private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Successfully captured the image
            // display in imageview
            previewImage();
        } else {
            // failed to capture image
            Toast.makeText(getApplicationContext(),
                    "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

private void previewImage() {
    try {
        // Bitmap factory
        BitmapFactory.Options options = new BitmapFactory.Options();

        // Downsizing image as it throws OutOfMemory exception for larger
        // images
        options.inSampleSize = 3;

        final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                options);

        photo.setImageBitmap(bitmap);

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

The problem that I encountered is that ... for some devices on which I tested the application, the application shows an empty preview of the snapshot, while in others the application works completely.

? , , , .

, .

+4
5

, Kitkat 4.4.2 Galaxy S4, , invalidate() ImageView, ImageBitmap. , . invalidate() setImageBitmap(), .

+2

:

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    //BitmapFactory.Options optionss = new BitmapFactory.Options();
    //optionss.inPreferredConfig = Bitmap.Config.RGB_565;


    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    BitmapFactory.decodeFile(path,options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(path, options);
}
public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

    final int halfHeight = height / 2;
    final int halfWidth = width / 2;

    // Calculate the largest inSampleSize value that is a power of 2 and keeps both
    // height and width larger than the requested height and width.
    while ((halfHeight / inSampleSize) > reqHeight
            && (halfWidth / inSampleSize) > reqWidth) {
        inSampleSize *= 2;
    }
}

return inSampleSize;}
+1

- FileUri, Uri, SharedPreferences. , :

public void takePhoto() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    fileUri = FileHelper.getOutputMediaFileUri();
    // Store uri to SharedPreferences
    pref.setImageUri(fileUri.toString());
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    startActivityForResult(intent, TAKE_PICTURE);
}

onActivityResult:

if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {
    // If user is taking photo then only call the SharedPreferences
    // If user is selecting photo from gallery, we can use the Intent data
    fileUri = Uri.parse(pref.getImageUri());
    if (fileUri.getPath().toString().length() < 1) {
        Toast.makeText(getApplicationContext(),
                "Sorry something went wrong ... Please try again",
                Toast.LENGTH_LONG).show();
    } else {
        String path = fileUri.getPath().toString();
        db_img_path = path;
        imageholder.setVisibility(View.VISIBLE);
        Bitmap bitmap = PathtoImage.previewImage(path);
        imagepreview.setImageBitmap(bitmap);
    }
}

:)

previewImage, , :

public static Bitmap previewImage(String path) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 4;
    final Bitmap bitmap = BitmapFactory.decodeFile(path, options);
    // Providing adjustment so that the image is shown in the correct orientation
    Matrix adjustment = adjustOrientation(path);
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
            bitmap.getWidth(), bitmap.getHeight(), adjustment, true);
    return resizedBitmap;
}

adjustOrientation, Matrix .

// Adjustment for orientation of images
public static Matrix adjustOrientation(String path) {
    Matrix matrix = new Matrix();
    try {
        ExifInterface exifReader = new ExifInterface(path);

        int orientation = exifReader.getAttributeInt(
                ExifInterface.TAG_ORIENTATION, -1);

        if (orientation == ExifInterface.ORIENTATION_NORMAL) {
            // do nothing
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            matrix.postRotate(90);
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            matrix.postRotate(180);
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            matrix.postRotate(270);
        }

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

    return matrix;
}

, - , , :)

0

, - .

profileImageView.post(new Runnable() {
                    @Override
                    public void run() {
                        profileImageView.setImageBitmap(bm);
                    }
                });

.

0

ImageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

0

All Articles