如何从Android图库中的图像中检索路径?

时间:2012-05-31 17:10:58

标签: android image path camera gallery

我有一个应用程序可以让你点击一个ImageButton,然后带你到Android图库或相机。我正在寻找的是一个onActivityResult方法,它从单击/拍摄的图像中检索图像路径并将其存储在字符串中。

有人能帮助我吗?这是打开图库/相机的onClick方法:

private void showImageDialog() {
    final String [] items = new String [] {"From Camera", "From SD Card"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Select Image");
    builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
        public void onClick( DialogInterface dialog, int item ) {
            if (item == 0) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File file = new File(Environment.getExternalStorageDirectory(),
                                    "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
                mImageCaptureUri = Uri.fromFile(file);

                try {
                    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
                    intent.putExtra("return-data", true);

                    startActivityForResult(intent, PICK_FROM_CAMERA);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                dialog.cancel();
            } else {
                Intent intent = new Intent();

                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);

                startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
            }
        }
    } );

    final AlertDialog dialog = builder.create();
    dialog.show();
}

1 个答案:

答案 0 :(得分:4)

使用此代码,

 ((Button) findViewById(R.id.Button01))
                .setOnClickListener(new OnClickListener() {

                    public void onClick(View arg0) {

                        // in onCreate or any event where your want the user to
                        // select a file
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(Intent.createChooser(intent,
                                "Select Picture"), SELECT_PICTURE);
                    }
                });


 public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);

                Log.v("IMAGE PATH====>>>> ",selectedImagePath);
            }
        }
    }

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

selectedImagePath是图片的路径。

相关问题