从Gallery中检索Image的路径到​​Edittext

时间:2014-05-16 01:38:27

标签: android android-intent gallery

我想在我的项目中实现以下目标:

  1. 点击Edittext打开Gallery
  2. 在图库中选择ImageFile,并将其路径输入我的edittext。
  3. 谁能告诉我怎么做?

2 个答案:

答案 0 :(得分:0)

onActivityResultUri uri = intent.getData();
然后以这种方式抓住uri路径 MyEditText.setText(uri.getPath());

答案 1 :(得分:0)

试试这个,

对于开放式图库,

Intent intent = new Intent();
                            intent.setType("image/*");
                            intent.setAction(Intent.ACTION_GET_CONTENT);
                            startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);

使用此方法获取图像路径,

@SuppressWarnings("unchecked")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {

                case PICK_IMAGE:

            String imagepath =  (getAbsolutePath(data.getData()) + "|").split("\\|");
                    break;
            }
        }
    }


public String getAbsolutePath(Uri uri) {
        String[] projection = { MediaColumns.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }