Android将视图保存到jpg或png
我想写一个安卓应用程序,基本上是在另一张图像上的图像上分层覆盖,然后我想把覆盖的图片保存为jpg或png。基本上这就是我想保存的整个视图 示例代码将非常有用 编辑: 我尝试了你的建议,但得到了一个空指针指向星号线Android将视图保存到jpg或png,android,camera,image,Android,Camera,Image,我想写一个安卓应用程序,基本上是在另一张图像上的图像上分层覆盖,然后我想把覆盖的图片保存为jpg或png。基本上这就是我想保存的整个视图 示例代码将非常有用 编辑: 我尝试了你的建议,但得到了一个空指针指向星号线 import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import android.app.Activity; import android.graph
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.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()方法的第三个参数。图像将以image.jpg的形式存储在sd卡的根目录中。将RelativeLayout或任何视图保存到图像(.jpg)
这上面什么都没有?我知道这是可能的,我在其他应用程序中也看到过。你能给我们一些代码来描述你是如何进行编辑的吗?现在这更像是一个想法,但我会让一个imageview覆盖在照片imageview上。除非有更好的方法。你想用图层保存图像(ala PSD)还是只保存平面图像(ala png)?一个平面png或jpg就可以了。我试过你的代码,但是FileOutputStream的根从哪里开始?因为我试图在emulator的文件夹中查找,但找不到保存的图像…它从phones根目录开始。因此,如果您想从SD卡加载某些内容,请使用Environment.getExternalStorageDirectory()获取SD卡的根目录。因此,在这种情况下,LinerLayout可以作为视图,并且它会在LinearLayout中捕获任何内容吗?看起来它可以正常工作,多亏了您,我的代表几乎都这样做了:)。谢谢大家!!我解决了我的问题。看见
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;
}