关于Android图片处理的总结

1.给图片创建存放的路径

首先要获取修改SD卡的权限

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
			if (getActivity().checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
				requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, CHOOSE_PICTURE);
			}
		}

创建文件夹

// 新建一个用来存储照片的文件夹
		File destDir = new File(Environment.getExternalStorageDirectory() + path);
		if (!destDir.exists()) {
			destDir.mkdirs();
		}

将图片保存到文件夹

File file = new File(path + "head1.jpg");
if (file.exists()){
	file.delete();
		}
try {
	out = new FileOutputStream(file);
	bitmap.compress(Bitmap.CompressFormat.JPEG,100,out);
	}catch (Exception e){
		e.printStackTrace();
		return;
		}finally {
			try {
				out.flush();
				out.close();
				}catch (Exception e){
					e.printStackTrace();
					return;
				}

将图片从路径取来放到界面

Bitmap bt = BitmapFactory.decodeFile(path + "head1.jpg");//已知的路径,将图片转成Bitmap

Drawable drawable = new BitmapDrawable(bt);//将Bitmap转化成drawable

personImg.setImageDrawable(drawable);//放放上图片


2.用Bade64对图片进行编解码

将图片转化成Bitmap并加密成String
private void uploadPic(Bitmap bitmap) {
Bitmap bt = BitmapFactory.decodeFile(path + "head1.jpg");//根据路径将图片转化为Bitmap
ByteArrayOutputStream bos = new ByteArrayOutputStream();//
//bt.compress(Bitmap.CompressFormat.JPEG,50,bos);//压缩
headpicture = Base64.encodeToString(bos.toByteArray(),Base64.DEFAULT);//转换成String
//在此可以将String上传服务器}

将String类型解码并放到布局中

Bitmap getBitmap = null;
try {
	byte[] bitmapArray;
	bitmapArray = Base64.decode(getheadpicString,Base64.DEFAULT);//getheadpicString为经过Base64加密过的图片
	getBitmap = BitmapFactory.decodeByteArray(bitmapArray,0,bitmapArray.length);//将String解码为Bitmap
	Drawable dra = new BitmapDrawable(getBitmap);//将Bitmap转化为Drawable
	personImg.setImageDrawable(dra);//放上图片
	}catch (Exception e){
		e.printStackTrace();
		}

以上就是目前我所接触的对图片处理的方法,希望大家指出错误,共同进步!






你可能感兴趣的:(android)