首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >已请求索引%1,大小为%3

已请求索引%1,大小为%3
EN

Stack Overflow用户
提问于 2016-07-27 17:25:39
回答 2查看 54关注 0票数 1

我创建了一个简单的应用程序,其中的图像存储从ImageView到数据库。但是当点击retrive按钮时,显示索引1请求的大小为3。我不知道出了什么问题。数据库类:

代码语言:javascript
复制
           @Override
    public void onCreate(SQLiteDatabase db) {
    String CREATE_IMAGE_TABLE = "CREATE TABLE " +TABLE_NAME  + "("
            + IMAGE_KEY + " BLOB )";
     db.execSQL(CREATE_IMAGE_TABLE);
     }
        @Override
       public void onUpgrade(SQLiteDatabase db, int i, int i1) {
     db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
     onCreate(db);

    }
     public  boolean insertData(byte[]image )throws SQLiteException
     {
    SQLiteDatabase db = this.getWritableDatabase();


    ContentValues cv=new ContentValues();
    cv.put(IMAGE_KEY,image);
    long result= db.insert(TABLE_NAME,null,cv);
    if(result==-1)
        return false;
    else
        return true;

   }
  public Cursor getAllData()
  {
    SQLiteDatabase db=this.getReadableDatabase();
    Cursor res=db.rawQuery("select * from "+TABLE_NAME,null);
    byte[]img=res.getBlob(0);
    return res;
  }

这是activity类:

代码语言:javascript
复制
           public void button2(View view)
            {
            try {

             Cursor res = myDb.getAllData();
            if (res ==null) {
            showMessage("error", "no data found");
            } else {
            StringBuffer buffer = new StringBuffer();
            while (res.moveToNext()) {
                buffer.append("id:" + res.getBlob(0) + "\n");
                byte[] image = res.getBlob(0);

                Bitmap bmp = BitmapFactory.decodeByteArray(image, 0,
                                 image.length); 


                imagee.setImageBitmap(bmp);

            }
            // showMessage("DATA", buffer.toString());
            }
           }
            catch (Exception e)
            {
          Toast.makeText(getBaseContext(),e.getMessage(),
                  Toast.LENGTH_LONG).show();
                    }}

              public  void buttonn(View view)
     {
      Bitmap bitmap = ((BitmapDrawable) imagee.getDrawable()).getBitmap();
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
      byte[] data = outputStream.toByteArray();
            boolean isInserted = myDb.insertData(data);

          if (isInserted == true)
        Toast.makeText(getBaseContext(), "Registration Succes!",
              Toast.LENGTH_SHORT).show();
            else 

           Toast.makeText(getBaseContext(), "No Record Registered!",
               Toast.LENGTH_SHORT).show();     

                }
                  }

我尝试了最多,但不能不做事情。我从res.movetoNext更改它,但显示相同的错误,并使用res.movetoFirst它也显示相同的错误

EN

回答 2

Stack Overflow用户

发布于 2016-07-27 17:30:03

Android sqlite CursorWindow有一个固定大小的缓冲区,在大多数配置中是2MB。您不能移动比这个更大的行。

不要在Android sqlite中存储像图像这样的大型二进制数据。改用外部存储,只需将路径保存在数据库中即可。

票数 2
EN

Stack Overflow用户

发布于 2016-07-28 15:20:17

查看以下代码,了解如何在android中保存图像:

代码语言:javascript
复制
private String saveToInternalStorage(Bitmap bitmapImage){
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
         // path to /data/data/yourapp/app_data/imageDir
        File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
        // Create imageDir
        File mypath=new File(directory,"profile.jpg");

        FileOutputStream fos = null;
        try {           
            fos = new FileOutputStream(mypath);
       // Use the compress method on the BitMap object to write image to the OutputStream
            bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        } catch (Exception e) {
              e.printStackTrace();
        } finally {
              fos.close(); 
        } 
        return directory.getAbsolutePath();
    }

您只需要将图像的路径添加到数据库中,而不是blob图像。

参考:Saving and Reading Bitmaps/Images from Internal memory in Android

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38608927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档