Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 可从imageview绘制到字节数组(数据库)_Android_Database_Imageview_Bytearray_Drawable - Fatal编程技术网

Android 可从imageview绘制到字节数组(数据库)

Android 可从imageview绘制到字节数组(数据库),android,database,imageview,bytearray,drawable,Android,Database,Imageview,Bytearray,Drawable,我一直在试图解决我的问题,寻找像这样的问题(),但我无法解决它 我正在从imageview中设置一个可绘制的图片(用户也可以更改),稍后我尝试获取该图片并将其转换为字节数组以将其保存在数据库中 我的代码: //imageview1.setImageResource(R.drawable.pictureJM); imageview1.setImageDrawable(getResources().getDrawable(R.drawable.pictureJM)); Drawable d1=im

我一直在试图解决我的问题,寻找像这样的问题(),但我无法解决它

我正在从imageview中设置一个可绘制的图片(用户也可以更改),稍后我尝试获取该图片并将其转换为字节数组以将其保存在数据库中

我的代码:

//imageview1.setImageResource(R.drawable.pictureJM);
imageview1.setImageDrawable(getResources().getDrawable(R.drawable.pictureJM));

Drawable d1=imageview1.getDrawable();
Bitmap bitmap =((BitmapDrawable)d1).getBitmap();  <-- (The application stops here)

ByteArrayOutputStream streamJM = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, streamJM);
byte[] imageInByte = streamJM.toByteArray();
//imageview1.setImageResource(R.drawable.pictureJM);
setImageDrawable(getResources().getDrawable(R.drawable.pictureJM));
Drawable d1=imageview1.getDrawable();
位图位图=((BitmapDrawable)d1.getBitmap() 试试这个:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pictureJM);
ByteArrayOutputStream streamJM = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, streamJM); 
byte[] imageInByte = streamJM.toByteArray();

我真的不明白它到底出了什么问题,但最后我解决了。我没有更改代码的任何其他部分,当我编写此代码时,它开始工作:

Bitmap bitmap = ((BitmapDrawable)imageview1.getDrawable()).getBitmap();
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte=stream.toByteArray();
谢谢你们所有人帮助我!
干杯

我尝试了与您输入的代码相同的代码,它工作正常。您到底遇到了什么错误?将资源放入
图像视图
而不是将资源直接解码为
位图
,这到底有什么意义
BitmapFactory.decodeResource(context.getResources(),R.drawable.pictureJM)
(Arshdeep_-Soal)-没有错误,它只是停止执行,但如果我使用其他函数从库中放入图片,我会让它工作。。。我不明白,很难解释。(sherpya),我该怎么处理这行代码?谢谢你们,我对这个问题感到头疼。这些代码行可以工作,但问题是用户可以更改imageview的图片,图片不是静态的,所以“R,drawable.pictureJM”只有一种情况是正确的。