得到画廊所有图像 [英] Get all Images in Gallery

查看:157
本文介绍了得到画廊所有图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发的,我想,当用户点击按钮选择图片,它应该是能够查看绘制文件夹的所有图像gallaryview或GridView控件的应用程序。我怎样才能做到这一点?然后点击来自电网或画廊观看图片后,我只是混帐的映像路径和存储到一个变量。我也希望,当我回到previous页我有我的映像路径的所有数据。


解决方案

 公共类GetImageActivity延伸活动{    私有静态最终诠释SELECT_PICTURE = 1;    私人字符串selectedImagePath;
    私人ImageView的IMG;    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        IMG =(ImageView的)findViewById(R.id.ImageView01);        ((按钮)findViewById(R.id.Button01))
                .setOnClickListener(新OnClickListener(){
                    公共无效的onClick(查看为arg0){
                        意向意图=新的Intent();
                        intent.setType(图像/ *);
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(Intent.createChooser(意向,选择图片),SELECT_PICTURE);
                    }
                });
    }    公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
        如果(结果code == RESULT_OK){
            如果(要求code == SELECT_PICTURE){
                乌里selectedImageUri = data.getData();
                selectedImagePath =的getPath(selectedImageUri);
                的System.out.println(映像路径:+ selectedImagePath);
                img.setImageURI(selectedImageUri);
            }
        }
    }    公共字符串的getPath(URI URI){
        的String [] =投影{MediaStore.Images.Media.DATA};
        光标光标= managedQuery(URI,投影,NULL,NULL,NULL);
        INT与Column_Index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        返回cursor.getString(Column_Index中);
    }
}

I am developing an application in which I want that When a user clicks on button "select image", it should be able to view all images of drawable folder in gallaryview or gridview. How can I do this? and after clicking on the image from grid or gallery view, I have to just fatch that image path and store into a variable. I also want that when I go back to the previous page I have all my data with the image path.

解决方案

public class GetImageActivity extends Activity {

    private static final int SELECT_PICTURE = 1;

    private String selectedImagePath;
    private ImageView img;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        img = (ImageView)findViewById(R.id.ImageView01);

        ((Button) findViewById(R.id.Button01))
                .setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) {
                        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);
                System.out.println("Image Path : " + selectedImagePath);
                img.setImageURI(selectedImageUri);
            }
        }
    }

    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);
    }
}

这篇关于得到画廊所有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆