如何在Android Studio中将imageview转换为字节数组? [英] How Can I convert an imageview to byte array in Android Studio?
本文介绍了如何在Android Studio中将imageview转换为字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的目标是获取用户选择的图片,并在其中填充图片视图.然后,在单击按钮时,该图像将被发送到Parse数据库.我知道我必须将imageview转换为字节数组,但是似乎不起作用.
My goal is to get a picture selected by the user and populate an image view with it. Then, on clicking a button, that image will be sent to a Parse database. I know I have to convert the imageview to byte array, but Doesn't seem to work.
任何帮助将不胜感激.这是我的代码:
Any help will be highly appreciated. here's my code:
//send the imageviwew to parse database
public void sendToParseBtn (View view){
Bitmap bitmapImage = findViewById (R.id.imageView);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(bitmapImage);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmapImage.compress(Bitmap.CompressFormat.JPEG, 40, stream);
byte[] byteArray = stream.toByteArray();
ParseFile file = new ParseFile("an.jpg",byteArray);
ParseObject object = new ParseObject("MyParseObject");
object.put("imageFile", file);
object.saveInBackground();
}
推荐答案
首先尝试将图像视图转换为可绘制的位图,然后获取byteArray:
Try converting the image view to a bitmap drawable first then get the byteArray:
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageInByte = baos.toByteArray();
//save your stuff
这篇关于如何在Android Studio中将imageview转换为字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文