Android的保存以JPG或PNG [英] Android save view to jpg or png
问题描述
我想编写一个Android应用程序,基本上层上的另一个图像的图像重叠,然后我想保存的图片与覆盖为JPG或PNG。基本上,这将是整个看法,我想保存。
样品code将是非常有益的。
编辑:
我尝试了您的建议和我得到一个空指针在出演线。
进口的java.io.File;
进口java.io.FileNotFoundException;
进口java.io.FileOutputStream中;
进口android.app.Activity;
进口android.graphics.Bitmap;
进口android.graphics.Bitmap.Com pressFormat;
进口android.os.Bundle;
进口android.os.Environment;
进口android.widget.LinearLayout;
进口android.widget.TextView;
公共类EditPhoto延伸活动{
/ **第一次创建活动时调用。 * /
的LinearLayout LL = NULL;
TextView的电视= NULL;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
电视=(TextView中)findViewById(R.id.text);
LL =(的LinearLayout)findViewById(R.id.layout);
ll.setDrawingCacheEnabled(真正的);
位图B = ll.getDrawingCache();
文件SD卡= Environment.getExternalStorageDirectory();
档案文件=新的文件(SD卡,image.jpg文件);
FileOutputStream中FOS;
尝试 {
FOS =新的FileOutputStream(文件);
*** b.com preSS(比较pressFormat.JPEG,95,FOS);
}赶上(FileNotFoundException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
}
您可以把视图的绘图缓存的优势。
view.setDrawingCacheEnabled(真正的);
位图B = view.getDrawingCache();
b.com preSS(比较pressFormat.JPEG,95,新的FileOutputStream(/一些/位置/ image.jpg文件));
在哪里查看您的看法。 95是的JPG COM pression质量。和文件输出流就是这样。
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.
Sample code would be very helpful.
EDIT:
I tried out your suggestions and am getting a null pointer at the 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();
}
}
}
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.
这篇关于Android的保存以JPG或PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!