2013-08-02 34 views
1

我試圖從數據庫加載圖像,但圖像不加載時,logcat的顯示空 是它與我編程的方式simethinf ..不能從SQLite數據庫加載圖像中的Android

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 
+0

你檢查過你的表返回字節數組嗎? –

+0

@ArmaanStranger可以給我這樣做的代碼嗎? –

+0

檢查你的數據庫好友。 「圖像」列中是否有名稱爲「vv」的圖像數據。 –

回答

0

我沒有找到一個方法將圖像保存到數據庫,所以我開始將圖像直接保存到內部存儲器,這不是答案這個問題,但可能會給一些想法,別人誰也沒有任何主意,做...

要保存到內部存儲器...

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如果u要解決的問題...

2

試試下面代碼..

byte[] Image_bytes = cursor.getBlob(cursor.getColumnIndex("image")); 
ImageView iv = (ImageView) findViewById(R.id.imgIcon); 
iv.setImageBitmap(new ImageConversation().convertArrayToBmp(Image_bytes)); 

...

public Bitmap convertArrayToBmp(byte[] array) { 
    Bitmap bitmap = BitmapFactory.decodeByteArray(array, 0, array.length); 
    return bitmap ; 
} 

使用下面的循環

c.moveToFirst(); 
while(!c.isAfterLast()) { 
    byte[] blob = c.getBlob(c.getColumnIndex("image")); 
    ImageView iv = (ImageView) findViewById(R.id.imgIcon); 
    iv.setImageBitmap(BitmapFactory.decodeByteArray(blob, 0,blob.length)); 
    c.moveToNext() 
} 
+0

它顯示錯誤ImageConversation()顯示,使一個新的類 –

+0

請給我詳細的logcat錯誤。 – 2013-08-02 05:25:56

+0

檢查你的同時循環..可能永遠不會結束。 – 2013-08-02 05:27:41

0

嘗試做這樣的事情 -

如果要存儲的圖像作爲字符串數據庫

if(image.isEmpty()) 
    { 
    System.out.println("is empty image"); 

    } 
    else 
    { 
    byte[] decodedByte = Base64.decode(image, 0); 
    Bitmap bm = BitmapFactory.decodeByteArray(decodedByte, 0, 
    decodedByte.length); 
    iv.setImageBitmap(bm); 
    } 
+0

,沒有解決問題.... –

+0

你可以發佈編輯的代碼? –

相關問題