ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

java-如何在SQLite android中保存和检索路径图像

2019-10-10 03:05:33  阅读:10  来源: 互联网

标签:android java sqlite



我想在我的应用程序中保存和检索图像路径

这是我的DBhelper

public void insert(String kdstore,String nama, String alamat, String kodepos, String notel, 
        String lng, String lat ,String Perus,String gambar){

    //waktu = new Date().toString();
    ContentValues cv = new ContentValues();
    cv.put("kdstore", kdstore);
    cv.put("nama", nama);
    cv.put("alamat", alamat);
    cv.put("kodepos", kodepos);
    cv.put("notel", notel);
    cv.put("lng", lng);
    cv.put("lat", lat);
    cv.put("Perus", Perus);
    cv.put("gambar", df.fileName);
    System.out.println();
    getWritableDatabase().insert("alfamapp", "name", cv);
}

这是我的startCameraActivity

_path=Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/";
        fileName =  String.format(System.currentTimeMillis()+".jpg");
        File file = new File(_path, fileName);
        try {
        file.createNewFile();
        } catch (IOException e) {
        e.printStackTrace();
        }
        Uri outputFileUri = Uri.fromFile(file);
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

        startActivityForResult(intent, IMAGE_CAPTURE); 

如何在我的数据库中保存来自cameraActivity的路径?
请帮我 :)

我把代码插入到startCamera方法上,这样的代码

public void startCamera() 
{
    Cursor c = helper.getById(almagId);
    c.moveToFirst();

    file = new File(_path, fileName);
    try {
    file.createNewFile();
    } catch (IOException e) {
    e.printStackTrace();
    }    

    file1 = new File(_path, fileName);
    try {
    file1.createNewFile();
    } catch (IOException e) {
    e.printStackTrace();
    }

    Uri outputFileUri = Uri.fromFile(file);
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    finishActivity(IMAGE_CAPTURE);

    Uri outputFileUri1 = Uri.fromFile(file1);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri1);
    startActivityForResult(intent, IMAGE_CAPTURE);
    System.out.println(file1);

    Dbhelper helper = new Dbhelper(this);
    SQLiteDatabase db = helper.getWritableDatabase();
    db.execSQL("update alfamapp set gambar ='"+file+"',gambar1 ='"+file1+"' " +
                "where kdstore='"+helper.getKdStore(c)+"'");
    db.execSQL("insert into image (kdstore,image1) values ('"+helper.getKdStore(c)+"','"+file+"')");
    db.execSQL("update image set image2 ='"+file1+"' where kdstore='"+helper.getKdStore(c)+"'");
    db.close();
    helper.close();
    load();

    System.out.println(db);

}

然后使用此代码检索此图像

if(helper.getGamb(c).equals("")){
        System.out.println("gamb kosong");
        detGam.setImageResource(R.drawable.gambarnf);

    }else detGam.setImageDrawable(Drawable.createFromPath(helper.getGamb(c)));

您对我的代码有解决方案?非常感谢:)

解决方法:

您可以在拍摄照片后使用onActivityResult保存路径.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if( requestCode == IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
        DBHelper dbHelper = new DBHelper(this);
        SQLiteDatabase sql = dbHelper.getWritableDatabase();
        sql.execSQL("insert statement for inserting path to database");
        sql.close();
        dbHelper.close();
    }
}


标签:android,java,sqlite

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有