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();
}
}
}
2 个解决方案
#1
77
You can take advantage of a View's drawing cache.
您可以利用View的绘图缓存。
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压缩的质量。而文件输出流就是这样。
#2
6
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()方法的第3个参数。图像将作为image.jpg存储在SD卡的根目录中。
#1
77
You can take advantage of a View's drawing cache.
您可以利用View的绘图缓存。
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压缩的质量。而文件输出流就是这样。
#2
6
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()方法的第3个参数。图像将作为image.jpg存储在SD卡的根目录中。