Android保存视图到jpg或png
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android保存视图到jpg或png相关的知识,希望对你有一定的参考价值。
我想写一个android应用程序,基本上在另一个图像上的图像上叠加,然后我想保存图片与叠加作为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();
}
}
}
答案
您可以利用View的绘图缓存。
view.setDrawingCacheEnabled(true);
Bitmap b = view.getDrawingCache();
b.compress(CompressFormat.JPEG, 95, new FileOutputStream("/some/location/image.jpg"));
视图是您的视图。 95是JPG压缩的质量。而文件输出流就是这样。
另一答案
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard, "image.jpg");
FileOutputStream fos = new FileOutputStream(file);
在Moncader的答案中使用fos引用作为b.compress()方法的第3个参数。图像将作为image.jpg存储在SD卡的根目录中。
另一答案
将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;
}
以上是关于Android保存视图到jpg或png的主要内容,如果未能解决你的问题,请参考以下文章
将 NSImageView 以 png/jpg 格式保存到磁盘
AS3:如何截取JPG/PNG截图并保存到Android手机的SDCard目录