Android - bitmap komprimieren vor dem speichern auf SDCARD in der Tätigkeit für das Ergebnis

Ich habe Zerschlagung meinen Kopf auf diese, und nicht so sicher, was zu tun ist. Was ich versuche zu tun ist: Nehmen Sie ein Bild, komprimieren Sie es in png (unter Beibehaltung der original-Abmessungen), und dann speichern Sie es auf sdCard. Der Grund, warum ich brauchen, um dies zu tun ist, denn ich habe zu komprimieren es wieder, dann Base64 Kodieren Sie es, so kann ich senden Sie es an einen server. Das problem ist 1. die Datei ist zu groß 2. ich bin running out of memory und 3. nicht sicher, ob ich das Tue richtig.

Vielen Dank für Ihre Hilfe

Hier ist mein code:

@Override
public void onClick(View button) {
    switch (button.getId()) {
        case R.id.cameraButton:
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(new File("/sdcard/test.png")));
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
            break;
        case R.id.galleryButton:
            sendToDatabase();
            break;
    }
}

//Camera on activity for result - save it as a bmp and place in imageview
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST) {
        //do something
    }

    if (resultCode == Activity.RESULT_OK) {
        Log.d(TAG, "result ok");

        picture = BitmapFactory.decodeFile("/sdcard/test.png");

        //Create string to place it in sd card
        String extStorageDirectory = Environment
                .getExternalStorageDirectory().toString();
        //create output stream
        OutputStream outputStream = null;
        //create file
        File file = new File(extStorageDirectory, "test.png");
        try {
            outputStream = new FileOutputStream(file);
            picture.compress(Bitmap.CompressFormat.PNG, 80, outputStream);
            //picture.recycle();
            outputStream.flush();
            outputStream.close();
        } catch (IOException e){
            Log.d(TAG, "ERROR");
        }
        imageView.setImageBitmap(picture);
    }
}

public void sendToDatabase() {
    InputStream inputStream = null;

    //get the picture from location
    picture = BitmapFactory.decodeFile("/sdcard/test.png");

    //CONVERT:
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    Boolean didItWork = picture.compress(Bitmap.CompressFormat.PNG, 50, outStream);
    picture.recycle();
    if (didItWork = true) {
        Log.d(TAG, "compression worked");
    }
    Log.d(TAG, "AFTER. Height: " + picture.getHeight() + " Width: "
        + picture.getWidth());
    final byte[] ba = outStream.toByteArray();
    try {
        outStream.close();
    } catch (IOException e1) {
        //TODO Auto-generated catch block
        e1.printStackTrace();
    }
}
Beachten Sie, dass die Bitmap.komprimieren(..) die Qualität von Einstellung hat keinen Effekt auf die PNG-Komprimierung. Wenn Sie die Dateigröße reduzieren, Ihre Optionen sind entweder die verlustbehaftete JPG-oder Drop-image-Größe.

InformationsquelleAutor Splitusa | 2011-12-28

Schreibe einen Kommentar