拍照并保存路径

时间:2014-04-30 11:44:17

标签: java android android-photos

我知道可能这个问题已经存在,但我找不到任何可以帮助我的事情。我想拍一张照片并保存它的路径。我已经拍摄了照片,但我无法在Toast中显示路径或将其保存在数据库中。

private static final int TAKE_PICTURE = 1;
private Uri outputFileUri;
SQLiteDatabase mydb;
static Uri capturedImageUri = null;
ImageView ecran;
Button b2,vertudo;
private String path;
ArrayList data;
ListView lista;

public void onClick(View v) {
            try{
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File file = new File(Environment.getExternalStorageDirectory(),"test.jpg");
            outputFileUri = Uri.fromFile(file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(intent, TAKE_PICTURE);
            }catch(Exception e){

            }

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    try{
        if (resultCode == RESULT_OK) {
            if (requestCode == TAKE_PICTURE) {
                outputFileUri = data.getData();
                path = getPath(outputFileUri);
                mydb.execSQL("INSERT INTO caminho(nome) VALUES('"+path+"');");
                Toast.makeText(getApplicationContext(), "Sucesso " + path,Toast.LENGTH_LONG).show();
            }
        }
    }catch(Exception e){
        Toast.makeText(getApplicationContext(),"Nao",Toast.LENGTH_LONG).show();
    }

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor =getContentResolver().query(uri, projection, null,null,null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

1 个答案:

答案 0 :(得分:0)

你想做什么 path = getPath(outputFileUri);

我认为 onActivityResult 中的 data.getData()将直接返回捕获图像的路径。