将Android视图保存为JPG或PNG

44

我想编写一个安卓应用程序,可以将一张图像叠加在另一张图像上,然后我想将带有叠加层的图片保存为jpg或png格式。基本上,这将是我想要保存的整个视图。

提供示例代码将非常有帮助。

编辑:

我尝试了你的建议,在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();
      }

        }
    }

这方面没有任何消息吗?我知道这是可能的,我在其他应用程序中看到过它。 - shaneburgess
你能给我们一些描述你正在进行编辑的代码吗? - Chris Thompson
现在这只是一个想法,但我会在照片ImageView上叠加一个ImageView。除非有更好的方法。 - shaneburgess
你想要保存带有图层的图片(类似于PSD),还是只保存为扁平的图片(类似于PNG)? - Chris Thompson
一个扁平的PNG或JPG就可以了。 - shaneburgess
3个回答

100

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

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

其中`view`是你的View对象,`95`是JPG压缩质量,而`file output stream`则是指输出文件流。


我尝试了你的代码,但是FileOutputStream的根目录在哪里?因为我尝试在模拟器的文件夹中查找保存的图像,但是找不到... - Sephy
2
它始于手机的根目录。因此,如果您想从SD卡加载某些内容,请使用Environment.getExternalStorageDirectory()获取SD卡的根目录。 - Moncader
3
看起来它起作用了,谢谢你,几乎所有的声望都归功于你 : )。谢谢大家!! - shaneburgess
在Android 4.3上,提供的代码仅捕获视图中实际可见的行。滚动到视图外部的文本不会被捕获。有人知道这个问题的解决方案吗? - kirsche40
1
我解决了我的问题。请查看我的解决方案 - kirsche40
显示剩余3条评论

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

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


6

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接