public class MainActivity extends Activity {
private Button button;
private ImageView imageViewOne;
private ImageView imageViewTwo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
imageViewOne = (ImageView) findViewById(R.id.iv_src);
imageViewTwo = (ImageView) findViewById(R.id.iv_dest);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("android.intent.action.PICK");
intent.addCategory("android.intent.categoty.DEFAULT");
intent.setType("image/*");
startActivityForResult(intent, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
imageViewOne.setImageURI(uri);
InputStream inputStream;
try {
inputStream = getContentResolver().openInputStream(uri);
// 创建一个原图的拷贝, 把拷贝的图片 放在iv_dest
// 原图对应的bitmap 注意:这个图片是只读的 不可以被修改.
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
// 创建一个可以被修改的位图资源,宽高信息 和 图片的类型 跟原图是一模一样
// 下面的代码创建的图片 是一个空白的图片
Bitmap bitmap2 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
// 拿着可以被修改的图片创建一个画布.
Canvas canvas = new Canvas(bitmap2);
Paint paint = new Paint();
canvas.drawBitmap(bitmap, new Matrix(), paint);
imageViewTwo.setImageBitmap(bitmap2);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
android Bitmap拷贝一张图片
最新推荐文章于 2021-05-27 05:10:52 发布