web-dev-qa-db-fra.com

Android enregistrer la vue en jpg ou png

Je voudrais écrire une application Android qui superpose essentiellement une superposition sur une image sur une autre image, puis je voudrais enregistrer l'image avec la superposition au format jpg ou png. Fondamentalement, ce sera le vue d'ensemble que je voudrais sauver.

Un exemple de code serait très utile.

ÉDITER:

J'ai essayé vos suggestions et je reçois un pointeur nul sur la ligne étoilée.

 import Java.io.File;
import Java.io.FileNotFoundException;
import Java.io.FileOutputStream;

import Android.app.Activity;
import Android.graphics.Bitmap;
import Android.graphics.Bitmap.CompressFormat;
import Android.os.Bundle;
import Android.os.Environment;
import Android.widget.LinearLayout;
import Android.widget.TextView;

    public class EditPhoto extends Activity {
        /** Called when the activity is first created. */
     LinearLayout ll = null;
     TextView tv = null;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            tv = (TextView) findViewById(R.id.text);
            ll = (LinearLayout) findViewById(R.id.layout);
            ll.setDrawingCacheEnabled(true);
            Bitmap b = ll.getDrawingCache();
            File sdCard = Environment.getExternalStorageDirectory();
            File file = new File(sdCard, "image.jpg");
            FileOutputStream fos;
      try {
       fos = new FileOutputStream(file);
       *** b.compress(CompressFormat.JPEG, 95,fos);
      } catch (FileNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }

        }
    }
39
shaneburgess

Vous pouvez profiter du cache de dessin d'une vue.

view.setDrawingCacheEnabled(true);
Bitmap b = view.getDrawingCache();
b.compress(CompressFormat.JPEG, 95, new FileOutputStream("/some/location/image.jpg"));

Où la vue est votre vue. Le 95 est la qualité de la compression JPG. Et le flux de sortie de fichier est juste cela.

82
Moncader
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard, "image.jpg");
FileOutputStream fos = new FileOutputStream(file);

Utilisez la référence fos comme 3e paramètre de la méthode b.compress () dans la réponse de Moncader. L'image sera stockée en tant que image.jpg dans le répertoire racine de votre carte SD.

6
plugmind

Enregistrer RelativeLayout ou toute vue sur l'image (.jpg)

String getSaveImageFilePath() {
    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), YOUR_FOLDER_NAME);
    // Create a storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(YOUR_FOLDER_NAME, "Failed to create directory");
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageName = "IMG_" + timeStamp + ".jpg";

    String selectedOutputPath = mediaStorageDir.getPath() + File.separator + imageName;
    Log.d(YOUR_FOLDER_NAME, "selected camera path " + selectedOutputPath);

    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());

    int maxSize = 1080;

    int bWidth = bitmap.getWidth();
    int bHeight = bitmap.getHeight();

    if (bWidth > bHeight) {
        int imageHeight = (int) Math.abs(maxSize * ((float)bitmap.getWidth() / (float) bitmap.getHeight()));
        bitmap = Bitmap.createScaledBitmap(bitmap, maxSize, imageHeight, true);
    } else {
        int imageWidth = (int) Math.abs(maxSize * ((float)bitmap.getWidth() / (float) bitmap.getHeight()));
        bitmap = Bitmap.createScaledBitmap(bitmap, imageWidth, maxSize, true);
    }
    view.setDrawingCacheEnabled(false);
    view.destroyDrawingCache();

    OutputStream fOut = null;
    try {
        File file = new File(selectedOutputPath);
        fOut = new FileOutputStream(file);

        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        fOut.flush();
        fOut.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return selectedOutputPath;
}
3
Harikesh