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

前端之家收集整理的这篇文章主要介绍了如何从Android图库中的图像中检索路径?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

private void showImageDialog() {
    final String [] items = new String [] {"From Camera","From SD Card"};
    ArrayAdapter
最佳答案
使用此代码,

 ((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);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
}

selectedImagePath是图像的路径.

原文链接:https://www.f2er.com/android/430618.html

猜你在找的Android相关文章