1

データベースから画像をロードしようとしましたが、画像がロードされていません.logcatはnullを示しています.

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
4

3 に答える 3

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()
}
于 2013-08-02T05:10:01.730 に答える
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);
  }
于 2013-08-02T05:44:23.760 に答える
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.os.networkonmainthreadexception」によるエラーを表示する新しい Android 用です。

問題を解決したい場合は、AsyncTask を使用することもできます...

于 2013-08-13T06:04:35.863 に答える