如何在Android中将图像转换为byte [] [英] How to convert Image into byte[] in Android
本文介绍了如何在Android中将图像转换为byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将图像保存在sqlite中.在这种方法中,我将图像路径转换为字符串.我的问题是如何将其转换为byte []格式.
I want to save image in sqlite. In this method I have got image path into string. My Question is How to convert it into byte[] format.
if (resultCode == RESULT_OK) {
// we need to update the gallery by starting MediaSanner service.
mScanner = new MediaScannerConnection(ImagePreviewActivity.this,new MediaScannerConnection.MediaScannerConnectionClient() {
public void onMediaScannerConnected() {
mScanner.scanFile(imageUri.getPath(), null );
}
public void onScanCompleted(String path, Uri uri) {
mScanner.disconnect();
if (path.equals(imageUri.getPath())) {
photoPath=imageUri.getPath();
ImagePreviewActivity.this.runOnUiThread(new Runnable() {
public void run() {
capture_photo = (ImageView) findViewById(R.id.caturePhoto);
Bitmap myBitmap = BitmapFactory.decodeFile(photoPath);
capture_photo.setImageBitmap(myBitmap);
}
});
mScanner.disconnect();
}
}
});
mScanner.connect();
推荐答案
首先将ImagePath转换为位图
First convert ImagePath to Bitmap
Bitmap src=BitmapFactory.decodeFile(imagePath);
使用以下代码将位图转换为字节数组:
Convert bitmap into byte array using this code:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
src.compress(Bitmap.CompressFormat.PNG, 100, baos);
data = baos.toByteArray();
在发表问题后发表评论
要在imageview中显示字节数组,请使用以下代码行:
To show byte array into imageview use these lines of code:
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);
imageView.setImageBitmap(bitmap);
要在imageView中显示imagePath,可以使用
To show imagePath into imageView you can use
Bitmap src=BitmapFactory.decodeFile(imagePath);
imageView.setImageBitmap(bitmap);
这篇关于如何在Android中将图像转换为byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文