8
votes

Le bitmap rotatif provoque une exception exceptionnelle

Je fais une rotation d'un bitmap de cette façon, sur chaque bouton, cliquez sur l'image Tourner à 90 degrés

Matrix matrix = new Matrix();
matrix.postRotate(90);
rotated = Bitmap.createBitmap(rotated, 0, 0,
        rotated.getWidth(), rotated.getHeight(), matrix, true);
iv.setImageBitmap(rotated);


3 Réponses :


13
votes

J'ai des suggestions pour vous.

1) Lorsque vous avez une tâche de la faim de mémoire, utilisez des méthodes et si possible avec asynctack code>.
2) Déclarez des objets sous forme faibles code>. Cela vous donnera la chance de libérer la mémoire après utilisation. Voir l'exemple ci-dessous. P>

public class RotateTask extends AsyncTask<Void, Void, Bitmap> {
    private WeakReference<ImageView> imgInputView;
    private WeakReference<Bitmap> rotateBitmap;

    public RotateTask(ImageView imgInputView){
        this.imgInputView = new WeakReference<ImageView>(imgInputView);
    }

    @Override
    protected void onPreExecute() {
        //if you want to show progress dialog
    }

    @Override
    protected Bitmap doInBackground(Void... params) {
        Matrix matrix = new Matrix();
        matrix.postRotate(90);
        rotateBitmap = new WeakReference<Bitmap>(Bitmap.createBitmap(rotated, 0, 0,rotated.getWidth(), rotated.getHeight(), matrix, true));
        return rotateBitmap.get();
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        //dismiss progress dialog
        imgInputView.get().setImageBitmap(result);
    }
}


2 commentaires

C'est un économiseur de vie à 4h00 du matin.


Content que ça vous a aidé .. :)



0
votes

Essayez comme ci-dessous:

Matrix matrix = new Matrix();
matrix.postRotate(90);
rotated = Bitmap.createBitmap(rotated, 0, 0,rotated.getWidth(), rotated.getHeight(), matrix, true);
ByteArrayOutputStream bmpStream = new ByteArrayOutputStream();
rotated.compress(Bitmap.CompressFormat.JPEG,100, bmpStream);
iv.setImageBitmap(rotated);


0 commentaires