我试图从数据库中加载图像,但图像未加载,logcat显示为空,是否为我编程的方式simethinf。
imgdisplay = (ImageView) findViewById(R.id.imgIcon);
myDB = this.openOrCreateDatabase("hello", MODE_PRIVATE, null);
Cursor c = myDB.rawQuery(
"SELECT image FROM employee_details WHERE name= 'vv'", null);
if (c.getCount() > 0) {
c.moveToFirst();
do {
byte[] blob = c.getBlob(c.getColumnIndex("image"));
ImageView iv = (ImageView) findViewById(R.id.imgIcon);
iv.setImageBitmap(BitmapFactory.decodeByteArray(blob, 0,blob.length));
} while (c.moveToNext());
}
c.close();
myDB.close();
Logcat错误是...
08-02 04:51:25.471: D/skia(8433): --- SkImageDecoder::Factory returned null
我没有找到将图像保存到数据库的方法,所以我开始将图像直接保存到内部存储器,这不是问题的答案,但可能会给一些也没有任何想法的人一些想法。去做...
要保存到内部存储器中...
File fileWithinMyDir = getApplicationContext().getFilesDir();
try
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
URL url = new URL("http://t2.gstatic.com /images?q=tbn:ANd9GcQjZgUffqqe2mKKb5VOrDNd-ZxD7sJOU7WAHlFAy6PLbtXpyQZYdw");
File file = new File( fileWithinMyDir.getAbsolutePath() + "/" +"sun.jpg");
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1)
{
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
}
catch (IOException e)
{
Log.e("download", e.getMessage());
}
从内部存储器加载图像。
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
mImgView1 = (ImageView) findViewById(R.id.mImgView1);
Bitmap bitmap = BitmapFactory.decodeFile(fileWithinMyDir.getAbsolutePath() + "/" +"sunn"+".file extension");
mImgView1.setImageBitmap(bitmap);
这是针对较新的android,由于“ android.os.networkonmainthreadexception”而显示错误
如果您想解决问题,也可以使用AsyncTask ...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句