您的位置:首页 > 其它

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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息