Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android pick an image from gallery in PreferenceScreen?

Tags:

java

android

how i can pick an image from gallery in android PreferenceScreen menu?

xml code of my PreferenceScreen menu:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <Preference
        android:key="select_image"
        android:title="select image">
</PreferenceScreen> 
like image 437
Badal Avatar asked Aug 26 '12 03:08

Badal


1 Answers

i finally found the answer

first set setOnPreferenceClickListener

Preference prefereces=findPreference("test");
        prefereces.setOnPreferenceClickListener (new Preference.OnPreferenceClickListener(){
            public boolean onPreferenceClick(Preference preference){
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                int PICK_IMAGE = 1;
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
                return true;
            }
        });

than Get it into onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

            if(resultCode == RESULT_OK){  
                Uri selectedImage = imageReturnedIntent.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();


                Log.d(LOG_TAG, "Data Recieved! " + filePath);

        }
like image 91
Badal Avatar answered Oct 20 '22 11:10

Badal