如何从SD卡检索图像并将其作为blob类型存储到sqlite db中

时间:2013-10-17 12:49:16

标签: android sqlite blob android-sdcard quickblox

我只是尝试从SD卡和网址获取图像,并将其作为blob类型存储到SQLITE数据库中。我真的很困惑逻辑如何做任何人都知道请帮助我。提前谢谢。

这是我从资产中的drawable文件夹中检索图像的代码。

 public class MainActivity extends Activity implements OnClickListener{

    private ImageView imageview=null;
    private Button btninsert=null;
    private Button btnretrive=null;
    private MyDataBase mdb=null;
    private SQLiteDatabase db=null;
    private Cursor c=null;
    private byte[] img=null;
    private static final String DATABASE_NAME = "ImageDb.db";
    public static final int DATABASE_VERSION = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btninsert=(Button)findViewById(R.id.button_insert);
        btnretrive= (Button)findViewById(R.id.button_retrieve);
        imageview= (ImageView)findViewById(R.id.imageView_image);
        imageview.setImageResource(0);
        btninsert.setOnClickListener(this);
        btnretrive.setOnClickListener(this);
        mdb=new MyDataBase(getApplicationContext(), DATABASE_NAME,null, DATABASE_VERSION);


        Bitmap b=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        b.compress(Bitmap.CompressFormat.PNG, 100, bos);
        img=bos.toByteArray();
        db=mdb.getWritableDatabase();
    }

    @Override
    public void onClick(View arg0) {

        if(btninsert==arg0)
        {
            ContentValues cv=new ContentValues();
            cv.put("image", img);
            db.insert("tableimage", null, cv);
            Toast.makeText(this, "inserted successfully", Toast.LENGTH_SHORT).show();
        }
        else if(btnretrive==arg0)
        {
                String[] col={"image"};
                c=db.query("tableimage", col, null, null, null, null, null);

                if(c!=null){
                    c.moveToFirst();
                    do{
                        img=c.getBlob(c.getColumnIndex("image"));
                       }while(c.moveToNext());
                }
                Bitmap b1=BitmapFactory.decodeByteArray(img, 0, img.length);

                 imageview.setImageBitmap(b1);
                 Toast.makeText(this, "Retrive successfully", Toast.LENGTH_SHORT).show();
            }
        }

}

3 个答案:

答案 0 :(得分:0)

private void saveToDB() {
        SQLiteDatabase myDb;
        String MySQL;
        byte[] byteImage1 = null;
        byte[] byteImage2 = null;
        MySQL = "create table emp1(_id INTEGER primary key autoincrement, sample TEXT not null, audio BLOB);";
        myDb = openOrCreateDatabase("Blob List", Context.MODE_PRIVATE, null);
        myDb.execSQL(MySQL);
        String s = myDb.getPath();
        textView.append("\r\n" + s + "\r\n");
        myDb.execSQL("delete from emp1");
        ContentValues newValues = new ContentValues();
        newValues.put("sample", "HI Hello");

        try {
            // FileInputStream instream = new
            // FileInputStream("/sdcard/TestRecordingDasa1/Record1975243059.amr");
            // BufferedInputStream bif = new BufferedInputStream(instream);
            // byteImage1 = new byte[bif.available()];
            // bif.read(byteImage1);

            InputStream is = new FileInputStream("/sdcard/TestRecordingDasa1/YOUR FILE NAME");
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int bytesRead;
            while ((bytesRead = is.read(b)) != -1) {
                bos.write(b, 0, bytesRead);

            }
            byte[] bytes = bos.toByteArray();

            textView.append("\r\n" + bytes.length + "\r\n");
            newValues.put("audio", bytes);

            long ret = myDb.insert("emp1", null, newValues);
            if (ret < 0)
                textView.append("\r\n!!! Error add blob failed!!!\r\n");
        } catch (IOException e) {
            textView.append("\r\n!!! Error: " + e + "!!!\r\n");
        }

        Cursor cur = myDb.query("emp1", null, null, null, null, null, null);
        cur.moveToFirst();
        while (cur.isAfterLast() == false) {
            textView.append("\r\n" + cur.getString(1) + "\r\n");
            cur.moveToNext();
        }
        // /////Read data from blob field////////////////////
        cur.moveToFirst();
        byteImage2 = cur.getBlob(cur.getColumnIndex("audio"));
        // bmImage.setImageBitmap(BitmapFactory.decodeByteArray(byteImage2, 0,
        // byteImage2.length));
        textView.append("\r\n" + byteImage2.length + "\r\n");

        cur.close();

        myDb.close();

    }

答案 1 :(得分:0)

尝试使用此代码从SD卡中检索图像。

String path = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/Foldername";
File[] pictures = file.listFiles();

使用此图片数组存储在db。

答案 2 :(得分:0)

此行显示从drawable获取位图

Bitmap b=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

在这里你可以从特定图像的sd卡获取bimap photo.png

File f = new File("/mnt/sdcard/photo.png");
Bitmap b = BitmapFactory.decodeFile(f.getAbsolutePath());