Android 将视图保存为 jpg 或 png

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3107527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:43:26  来源:igfitidea点击:

Android save view to jpg or png

androidcameraimage

提问by shaneburgess

I would like to write an android app that basically layers an overlay on image on another image and then I would like to save the picture with the overlay as a jpg or png. Basically this will be the whole view that I would like to save.

我想编写一个 android 应用程序,它基本上在另一个图像上的图像上叠加一个叠加层,然后我想将带有叠加层的图片保存为 jpg 或 png。基本上这将是我想保存的整个视图。

Sample code would be very helpful.

示例代码会很有帮助。

EDIT:

编辑:

I tried out your suggestions and am getting a null pointer at the Starred Line.

我尝试了您的建议,并在 Starred Line 处得到了一个空指针。

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

        }
    }

回答by Moncader

You can take advantage of a View's drawing cache.

您可以利用视图的绘图缓存。

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

Where view is your View. The 95 is the quality of the JPG compression. And the file output stream is just that.

视图是您的视图。95 是 JPG 压缩的质量。文件输出流就是这样。

回答by plugmind

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

Use fos reference as a 3rd parameter of b.compress() method in Moncader's answer. The image will be stored as image.jpg in root directory of your sd card.

在 Moncader 的回答中使用 fos 参考作为 b.compress() 方法的第三个参数。图像将作为 image.jpg 存储在 SD 卡的根目录中。

回答by Harikesh

Save RelativeLayout or any view to image (.jpg)

将 RelativeLayout 或任何视图保存到图像 (.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;
}