8
votes

ImageView SetImageBitMap ne fonctionne pas sur certains appareils

Je pratiquais avec l'API Camera Code> pour laquelle j'ai procédé ce qui suit:

a. Configurez un répertoire fort> pour l'image capturée (pour startactivityforresult code>) p>

b. Configurez le bitmap de manière à ce que l'image puisse être affichée une fois enregistrée dans l'application elle-même. P>

Voici le code pour les éléments suivants: p>

Configuration du répertoire. P>

// 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();
    }
}


0 commentaires

6 Réponses :


0
votes

Une façon dont j'ai eu autour de ce problème était lors de la définition du fileuri code>, j'ai enregistré le uri code> à l'aide de SharedPreferences code>. Donc, dans mon code: xxx pré>

dans mon OnActiVIVIVIVIVIVITURE CODE> Callback: P>

// 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 commentaires

1
votes

Essayez de charger le bitmap efficacement:

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;}


0 commentaires

2
votes

au moins pour KITKAT 4.4.2 sur une galaxie S4, avec une mise en page relative, je devais appeler invalidate () sur l'image de l'image que je viens de setImageBitmap sur. Si je ne l'ai pas fait, j'ai eu l'écran vide. Après avoir ajouté l'invalidation () après SetImageBitMap (), j'ai eu l'image.


0 commentaires

1
votes

En réalité, il est en train de régler mais cela n'apparaît pas pour une raison quelconque. XXX

Cela fonctionne pour moi.


0 commentaires

3
votes

J'ai eu le même problème et je l'ai résolu en modifiant le rendu de la vue sur le logiciel

imageview.setlayertype (vue.layer_type_software, null);


2 commentaires

C'est des œuvres, mais je n'ai pas compris la raison pour laquelle mon image montrait avec rotation à 90 degrés ...


Vous devez lire l'orientation de la caméra pour résoudre ce problème



0
votes

Dans mon cas, il est corrigé en ajoutant Android: Laperype = "Logiciel" en XML.

<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layerType="software"
    android:src="@drawable/placeholder"/>


0 commentaires