如何保存创建的位图?
How to save a created bitmap?
我想在单击按钮后保存创建的位图
我怎样才能做到这一点?如果可能,到特定位置
这是我的代码:
quoteimage.requestLayout();
if (quoteimage.getViewTreeObserver().isAlive()) {
quoteimage.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
// we check which build version we are using
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
quoteimage.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
quoteimage.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
viewToBitmap(quoteimage); ///this one creates the bitmap
}
});
}
share.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
///Here I want to save the bitmap
}
});
使用此方法保存您的位图,也可以像您一样传递文件名和位置
private createDirectoryAndSaveFile(Bitmap imageToSave, String fileName,String location) {
File direct = new File(Environment.getExternalStorageDirectory() + "/"+location);
if (!direct.exists()) {
File wallpaperDirectory = new File("/sdcard/"+location+"/");
wallpaperDirectory.mkdirs();
}
File file = new File(new File("/sdcard/"+location+"/"), fileName+".jpg");
if (file.exists()) {
file.delete();
}
try {
FileOutputStream out = new FileOutputStream(file);
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
File externalFile = new File(Environment.getExternalStorageDirectory(),"your/location/"+fileName+".jpg");
externaluri = Uri.parse(externalFile.getPath());
Log.d("externaluri", externaluri.toString());
}
示例
createDirectoryAndSaveFile(yourbitmap, "your_image_name" ,"your/location");
在onClick()中调用save():
protected void save(){
FileOutputStream out = null;
try {
out = new FileOutputStream(filename);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}}
Android Saving created bitmap to directory on sd card
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
_bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
//you can create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "test.jpg")
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();
我想在单击按钮后保存创建的位图
我怎样才能做到这一点?如果可能,到特定位置
这是我的代码:
quoteimage.requestLayout();
if (quoteimage.getViewTreeObserver().isAlive()) {
quoteimage.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
// we check which build version we are using
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
quoteimage.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
quoteimage.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
viewToBitmap(quoteimage); ///this one creates the bitmap
}
});
}
share.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
///Here I want to save the bitmap
}
});
使用此方法保存您的位图,也可以像您一样传递文件名和位置
private createDirectoryAndSaveFile(Bitmap imageToSave, String fileName,String location) {
File direct = new File(Environment.getExternalStorageDirectory() + "/"+location);
if (!direct.exists()) {
File wallpaperDirectory = new File("/sdcard/"+location+"/");
wallpaperDirectory.mkdirs();
}
File file = new File(new File("/sdcard/"+location+"/"), fileName+".jpg");
if (file.exists()) {
file.delete();
}
try {
FileOutputStream out = new FileOutputStream(file);
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
File externalFile = new File(Environment.getExternalStorageDirectory(),"your/location/"+fileName+".jpg");
externaluri = Uri.parse(externalFile.getPath());
Log.d("externaluri", externaluri.toString());
}
示例
createDirectoryAndSaveFile(yourbitmap, "your_image_name" ,"your/location");
在onClick()中调用save():
protected void save(){
FileOutputStream out = null;
try {
out = new FileOutputStream(filename);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}}
Android Saving created bitmap to directory on sd card
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
_bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
//you can create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "test.jpg")
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();