android bitmap文件修改保存

将文件中的图片拿出来,设置为固定大小并保存
方法一:
public Bitmap convertToBitmap(String path, int w, int h) {
			BitmapFactory.Options opts = new BitmapFactory.Options();
			// 设置为ture只获取图片大小
			opts.inJustDecodeBounds = true;
			opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
			// 返回为空
			BitmapFactory.decodeFile(path, opts);
			int width = opts.outWidth;
			int height = opts.outHeight;
			float scaleWidth = 0.f, scaleHeight = 0.f;
			if (width > w || height > h) {
				// 缩放
				scaleWidth = ((float) width) / w;
				scaleHeight = ((float) height) / h;
			}
			opts.inJustDecodeBounds = false;
			float scale = Math.max(scaleWidth, scaleHeight);
			opts.inSampleSize = (int) scale;
			WeakReference<Bitmap> weak = new WeakReference<Bitmap>(
					BitmapFactory.decodeFile(path, opts));
			return Bitmap.createScaledBitmap(weak.get(), w, h, true);
		}

2.方法二
//-------------------保存缩略
Bitmap bitmap =BitmapFactory.decodeFile(saveFilePate);
saveFile(zoomImage(bitmap, 200, 150), savePath ,filename);

// 产生缩略图
	private Bitmap zoomImage(Bitmap bgimage, int newWidth, int newHeight) {
		int width = bgimage.getWidth();
		int height = bgimage.getHeight();
		Matrix matrix = new Matrix();
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, width, height,
				matrix, true);
		return bitmap;
	}
//保存方法
	private void saveFile(Bitmap bm, String path, String fileName)
			throws IOException {
		File dirFile = new File(path);
		if (!dirFile.exists()) {
			dirFile.mkdir();
		}
		File myCaptureFile = new File(path + "/" + fileName);
		if (myCaptureFile.exists()) {
			myCaptureFile.delete();
		}
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream(myCaptureFile));
		bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
		bos.flush();}
		


Android 4.4及以上版本(API级别19),你可以使用`Bitmap.compress()`方法将Bitmap对象保存文件。以下是基本步骤: 1. 创建一个File对象,指定你要保存文件的位置和名称: ```java File outputImageFile = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "example.jpg"); ``` 这里假设你想在设备的图片库目录下创建一个名为"example.jpg"的新文件。`getExternalFilesDir(Environment.DIRECTORY_PICTURES)`获取外部存储的图片目录。 2. 使用合适的压缩格式(例如PNG、JPEG等)和质量(范围0-100,100表示最高质量但文件较大,0表示最小质量但文件更小)来压缩Bitmap: ```java Bitmap.CompressFormat format = Bitmap.CompressFormat.JPEG; // 或其他格式,比如Bitmap.CompressFormat.PNG int quality = 100; // 质量调整,范围0-100 try { FileOutputStream fos = new FileOutputStream(outputImageFile); bitmap.compress(format, quality, fos); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } ``` 这里使用了JPEG压缩格式,并设置了质量为100。如果需要更改压缩格式或质量,只需修改`format`和`quality`变量即可。 3. 最后别忘了关闭文件流以释放资源: 完整示例: ```java Bitmap bitmap = ... // 获取你的Bitmap对象 try { File outputImageFile = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "example.jpg"); FileOutputStream fos = new FileOutputStream(outputImageFile); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.close(); Log.i("SaveSuccess", "Bitmap saved successfully."); } catch (Exception e) { Log.e("SaveError", "Failed to save bitmap: ", e); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值