Bitmap 传值

我想把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();
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值