如何保存在内部存储的位图 [英] How to save a bitmap on internal storage
本文介绍了如何保存在内部存储的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的code我,我要保存该位图上的我的内部存储。公共布尔saveImageToInternalStorage是谷歌一个code,但我不知道如何使用它。当我触摸按钮2跟随Button1的行动。
公共类MainActivity扩展活动实现OnClickListener {
按钮BTN,BTN1;
SurfaceView SV;
点阵位图;
帆布油画;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
BTN =(按钮)findViewById(R.id.button1);
BTN1 =(按钮)findViewById(R.id.button2);
SV =(SurfaceView)findViewById(R.id.surfaceView1);
btn.setOnClickListener(本);
btn1.setOnClickListener(本);
位= BitmapFactory.de codeResource(getResources(),R.drawable.ic_launcher);
}
@覆盖
公共无效的onClick(视图v){
帆布= sv.getHolder()lockCanvas()。
如果(帆布== NULL)回报;
canvas.drawBitmap(位图,100,100,NULL);
sv.getHolder()unlockCanvasAndPost(画布)。
}
公共布尔saveImageToInternalStorage(位图图像){
尝试 {
//使用COM preSS法的位图对象上写图像
//了的OutputStream
FileOutputStream中FOS = openFileOutput(desiredFilename.png,Context.MODE_PRIVATE);
//写入位图输出流
image.com preSS(Bitmap.Com pressFormat.PNG,100,FOS);
fos.close();
返回true;
}赶上(例外五){
Log.e(saveToInternalStorage(),e.getMessage());
返回false;
}
}
}
解决方案
要保存您的位图的SD卡使用下面的code
专卖店形象
私人无效storeImage(位图图像){
文件PictureFile的= getOutputMediaFile();
如果(PictureFile的== NULL){
Log.d(TAG,
错误创建的媒体文件,检查存储权限:); // e.getMessage());
返回;
}
尝试 {
FileOutputStream中FOS =新的FileOutputStream(PictureFile的);
image.com preSS(Bitmap.Com pressFormat.PNG,90,FOS);
fos.close();
}赶上(FileNotFoundException异常E){
Log.d(TAG,找不到文件:+ e.getMessage());
}赶上(IOException异常E){
Log.d(TAG,访问文件时出错:+ e.getMessage());
}
}
为了获取图像存储的路径
/ **创建一个文件用于保存图像或视频* /
私人文件getOutputMediaFile(){
//为了安全起见,你应该检查SD卡安装
//在此之前使用Environment.getExternalStorageState()。
文件mediaStorageDir =新的文件(Environment.getExternalStorageDirectory()
+/安卓/数据/
+ getApplicationContext()。getPackageName()
+/文件);
//这个位置效果最好,如果你希望共享的创建的图像
//应用程序之间,坚持后,您的应用程序已被卸载。
//创建存储目录,如果它不存在
如果(!mediaStorageDir.exists()){
如果(!mediaStorageDir.mkdirs()){
返回null;
}
}
//创建一个媒体文件名
字符串的timeStamp =新的SimpleDateFormat(ddMMyyyy_HHmm)格式(新的Date())。
文件媒体文件;
字符串mImageName =MI _+的timeStamp +JPG;
媒体文件=新的文件(mediaStorageDir.getPath()+文件分割符+ mImageName);
返回媒体文件;
}
修改 从您的意见我已编辑在此的onclick视图中的按钮1和按钮2的功能将分别执行。
公开的onClick(视图v){
开关(v.getId()){
案例R.id.button1:
//你的按钮1功能
打破;
案例R.id.按钮2:
//你的按钮2功能
打破;
}
}
this is my code I and I want to save this bitmap on my internal storage. The public boolean saveImageToInternalStorage is a code from google but I don't know how to use it. when I touch button2 follow the button1 action.
public class MainActivity extends Activity implements OnClickListener {
Button btn, btn1;
SurfaceView sv;
Bitmap bitmap;
Canvas canvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button1);
btn1=(Button)findViewById(R.id.button2);
sv=(SurfaceView)findViewById(R.id.surfaceView1);
btn.setOnClickListener(this);
btn1.setOnClickListener(this);
bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
}
@Override
public void onClick(View v) {
canvas=sv.getHolder().lockCanvas();
if(canvas==null) return;
canvas.drawBitmap(bitmap, 100, 100, null);
sv.getHolder().unlockCanvasAndPost(canvas);
}
public boolean saveImageToInternalStorage(Bitmap image) {
try {
// Use the compress method on the Bitmap object to write image to
// the OutputStream
FileOutputStream fos = openFileOutput("desiredFilename.png", Context.MODE_PRIVATE);
// Writing the bitmap to the output stream
image.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
return true;
} catch (Exception e) {
Log.e("saveToInternalStorage()", e.getMessage());
return false;
}
}
}
解决方案
To Save your bitmap in sdcard use the following code
Store Image
private void storeImage(Bitmap image) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
Log.d(TAG,
"Error creating media file, check storage permissions: ");// e.getMessage());
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
image.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
To Get the Path for Image Storage
/** Create a File for saving an image or video */
private File getOutputMediaFile(){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
+ "/Android/data/"
+ getApplicationContext().getPackageName()
+ "/Files");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
File mediaFile;
String mImageName="MI_"+ timeStamp +".jpg";
mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);
return mediaFile;
}
EDIT From Your comments i have edited the onclick view in this the button1 and button2 functions will be executed separately.
public onClick(View v){
switch(v.getId()){
case R.id.button1:
//Your button 1 function
break;
case R.id. button2:
//Your button 2 function
break;
}
}
这篇关于如何保存在内部存储的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文