Bitmap 传值
2016-03-31 11:31
260 查看
我想把A界面的Bitmap传递到B界面怎么办?
Intent intent = new Intent(A.thist,B.class);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, baos);
byte [] bitmapByte =baos.toByteArray();
intent.putExtra("bitmap", bitmapByte);
startActivity(intent);
上篇文章我自定义的裁剪框得到裁剪后的图片很大,这个方法就不行了。图片过大
为此我只能先把获得的图片先保存到SD卡中然后在另一个界面取出来。
public class ImageActivity extends ActionBarActivity {
private MyImage img;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
img = (MyImage) findViewById(R.id.img);
btn = (Button) findViewById(R.id.btn_save);
btn.setOnClickListener(l);
}
OnClickListener l = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(ImageActivity.this, ShowActivity.class);
String path = getSDCardPath();
// 保存到Sd卡中
savePhotoToSDCard(img.getBitmap(), path, "aaa", 100);
startActivity(intent);
Log.e("", "跳转界面...DD");
}
};
/**
* Save image to the SD card
*
* @param photoBitmap
* @param photoName
* @param path
*/
public static void savePhotoToSDCard(Bitmap photoBitmap, String path,
String photoName, int quality) {
if (checkSDCardAvailable()) {
File dir = new File(path);// 建一个文件
if (!dir.exists()) {// 如果文件夹不存在
dir.mkdirs();// 就创建个文件夹
}
Log.e("filepath....", "filepath...." + path + "/" + photoName
+ ".png");
File photoFile = new File(path + "/" + photoName + ".png");// 创建一个新的文件使用指定的文件夹和名称
FileOutputStream fileOutputStream = null;// 文件输出流
try {
fileOutputStream = new FileOutputStream(photoFile);
if (photoBitmap != null) {// 如果photoBitmap不为空的话,就将图片压缩到流中
if (photoBitmap.compress(Bitmap.CompressFormat.PNG,// 以JPEG的格式压缩到fileOutputStream中
quality, fileOutputStream)) {
fileOutputStream.flush();// 刷新
// fileOutputStream.close();
}
}
} catch (FileNotFoundException e) {
photoFile.delete();
e.printStackTrace();
} catch (IOException e) {
photoFile.delete();
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Check the SD card
*
* @return
*/
public static boolean checkSDCardAvailable() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
/**
* 获取SDCard的目录路径功能
*
* @return
*/
private String getSDCardPath() {
String path = null;
// 判断SDCard是否存在
boolean sdcardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
if (sdcardExist) {
path = Environment.getExternalStorageDirectory().getPath();
}
return path.toString();
}
}取出图片:
public class ShowActivity extends ActionBarActivity {
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
img = (ImageView) findViewById(R.id.img);
Bitmap bitmap = getPhotoFromSDCard(getSDCardPath(),"aaa");// 从sd卡中取出图片
img.setImageBitmap(bitmap);
}
/**
* Get images from SD card by path and the name of image
*
* @param photoName
* @return
*/
public static Bitmap getPhotoFromSDCard(String path, String photoName) {
File file = new File(path+"/"+photoName+".png");
InputStream is = null;
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap photoBitmap = BitmapFactory.decodeStream(is);
if (photoBitmap == null) {
return null;
} else {
return photoBitmap;
}
}
/**
* 获取SDCard的目录路径功能
* @return
*/
private String getSDCardPath(){
String path = null;
//判断SDCard是否存在
boolean sdcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(sdcardExist){
path = Environment.getExternalStorageDirectory().getPath();
}
return path.toString();
}
}
Intent intent = new Intent(A.thist,B.class);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, baos);
byte [] bitmapByte =baos.toByteArray();
intent.putExtra("bitmap", bitmapByte);
startActivity(intent);
上篇文章我自定义的裁剪框得到裁剪后的图片很大,这个方法就不行了。图片过大
为此我只能先把获得的图片先保存到SD卡中然后在另一个界面取出来。
public class ImageActivity extends ActionBarActivity {
private MyImage img;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
img = (MyImage) findViewById(R.id.img);
btn = (Button) findViewById(R.id.btn_save);
btn.setOnClickListener(l);
}
OnClickListener l = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(ImageActivity.this, ShowActivity.class);
String path = getSDCardPath();
// 保存到Sd卡中
savePhotoToSDCard(img.getBitmap(), path, "aaa", 100);
startActivity(intent);
Log.e("", "跳转界面...DD");
}
};
/**
* Save image to the SD card
*
* @param photoBitmap
* @param photoName
* @param path
*/
public static void savePhotoToSDCard(Bitmap photoBitmap, String path,
String photoName, int quality) {
if (checkSDCardAvailable()) {
File dir = new File(path);// 建一个文件
if (!dir.exists()) {// 如果文件夹不存在
dir.mkdirs();// 就创建个文件夹
}
Log.e("filepath....", "filepath...." + path + "/" + photoName
+ ".png");
File photoFile = new File(path + "/" + photoName + ".png");// 创建一个新的文件使用指定的文件夹和名称
FileOutputStream fileOutputStream = null;// 文件输出流
try {
fileOutputStream = new FileOutputStream(photoFile);
if (photoBitmap != null) {// 如果photoBitmap不为空的话,就将图片压缩到流中
if (photoBitmap.compress(Bitmap.CompressFormat.PNG,// 以JPEG的格式压缩到fileOutputStream中
quality, fileOutputStream)) {
fileOutputStream.flush();// 刷新
// fileOutputStream.close();
}
}
} catch (FileNotFoundException e) {
photoFile.delete();
e.printStackTrace();
} catch (IOException e) {
photoFile.delete();
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Check the SD card
*
* @return
*/
public static boolean checkSDCardAvailable() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
/**
* 获取SDCard的目录路径功能
*
* @return
*/
private String getSDCardPath() {
String path = null;
// 判断SDCard是否存在
boolean sdcardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
if (sdcardExist) {
path = Environment.getExternalStorageDirectory().getPath();
}
return path.toString();
}
}取出图片:
public class ShowActivity extends ActionBarActivity {
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
img = (ImageView) findViewById(R.id.img);
Bitmap bitmap = getPhotoFromSDCard(getSDCardPath(),"aaa");// 从sd卡中取出图片
img.setImageBitmap(bitmap);
}
/**
* Get images from SD card by path and the name of image
*
* @param photoName
* @return
*/
public static Bitmap getPhotoFromSDCard(String path, String photoName) {
File file = new File(path+"/"+photoName+".png");
InputStream is = null;
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap photoBitmap = BitmapFactory.decodeStream(is);
if (photoBitmap == null) {
return null;
} else {
return photoBitmap;
}
}
/**
* 获取SDCard的目录路径功能
* @return
*/
private String getSDCardPath(){
String path = null;
//判断SDCard是否存在
boolean sdcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(sdcardExist){
path = Environment.getExternalStorageDirectory().getPath();
}
return path.toString();
}
}
相关文章推荐
- c#窗体传值用法实例详解
- C#简单实现子窗体向父窗体传值的方法
- ASP.NET页面间的传值的几种方法
- 关于C语言中参数的传值问题
- C# Winform窗口之间传值的多种方法浅析
- php下通过POST还是GET来传值
- JS中Iframe之间传值的方法
- JavaScript实现同一页面内两个表单互相传值的方法
- asp.net 页面间传值方法小结
- Oracle索引(B*tree与Bitmap)的学习总结
- ASP.NET页面传递值的方式介绍
- Servlet与JSP间的两种传值情况
- javascript中的变量是传值还是传址的?
- Javascript showModalDialog两个窗体之间传值
- Android学习笔记--使用剪切板在Activity中传值示例代码
- 如何使用Matrix对bitmap的旋转与镜像水平垂直翻转
- 如何传值在2个页面之间 要求不刷新父页面,并且不能用Querystring传值
- JavaScript新窗口与子窗口传值详解
- 浅析Java方法传值和传引用问题
- C#实现窗体间传值实例分析