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

标签 java android 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();
    }
}

关于java - 如何在 SQLite android 中保存和检索路径图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9155105/

相关文章:

java - 使用 spring 3 security 在 jsp 页面中访问我的自定义用户对象

java - 未定义 [service.NewsServiceImpl] 类型的合格 bean

android - 查询 Parse 数据库以从 Parse 数据类中的单个列获取数据

android - 如何解决关于 "Use of SMS or Call Log permission groups"的 google play 警告

java - Apache Cayenne defaultCatalog 由环境变量设置

java - 有没有更好的方法来确定 computeIfAbsent 是否返回了新值?

android - 在轮廓openCV中查找直线

sqlite获取ROWID

ios - 多个查询未在 FMDB 中运行

c++ - Qt:保存 Qt-creator 应用程序的 SQLite 数据库的最佳 "Path"在哪里?