开发者

retrieve image from database

I got null when I am trying to convert bytearray into bitmap. I don't know why. please give me useful suggestions.

    super.onCreate(savedInstanceState);
    setContentView(R.layout.newscreen);
    TextView name = (TextView)findViewById(R.id.textView1);
    TextView age = (TextView)findViewById(R.id.textView2);开发者_JS百科
    ImageView img = (ImageView)findViewById(R.id.imageView1);

    db = openOrCreateDatabase(
            "StudentData.db"
            , SQLiteDatabase.CREATE_IF_NECESSARY
            , null
        );

    db.setVersion(1);
    db.setLocale(Locale.getDefault());
    db.setLockingEnabled(true);

    Cursor cur = db.query("stuData", 
                null, null, null, null, null, null);
            //cur.moveToFirst();
    cur.moveToFirst();
        //name.setText(cur.getString(1));
        //age.setText(cur.getString(2));

       // if(bb!=null)
        //{


        //ByteArrayInputStream imageStream = new ByteArrayInputStream(bb);
            //bs = new BufferedInputStream(bb)

        //img.setImageBitmap(theImage);
    while (cur.isAfterLast() == false) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize=5;
            name.append("n" + cur.getString(1));
            byte[] bb = cur.getBlob(3);


            System.out.print("DAta"+bb);

            for(int i =0;i<bb.length;i++)
            {
                System.out.println(bb[i]);
            }
            Bitmap theImage = BitmapFactory.decodeByteArray(cur.getBlob(3),0,cur.getBlob(3).length);

            img.setImageBitmap(theImage);

            cur.moveToNext();
        }
    cur.close();


there are few points to check.

  1. are you inserting the correct image byte array to DB? check the length of array
  2. On retriving from DB, check the length of array retrived.
  3. Also check weather the array is null or not.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜