how to get selected image from gallery in android?

后端 未结 4 906
梦谈多话
梦谈多话 2020-12-18 15:58

I have a problem: I have a function that brings me the image from the gallery ;but when I select the image, I don\'t get it.

public void funzione(View v){
in         


        
4条回答
  •  抹茶落季
    2020-12-18 16:14

    Declare Global Variables just after your class declaration at the top:

     private static int RESULT_LOAD_IMAGE = 1;
     private static final int PICK_FROM_GALLERY = 2;
     Bitmap thumbnail = null; 
    

    Call the intent like this: (Your funizone() function)

       public void funzione(){
           Intent in = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                          startActivityForResult(in, RESULT_LOAD_IMAGE);
    
       }
    

    Handle the result like this: Declare this outside of your onCreate anywhere in the class.

     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    
        super.onActivityResult(requestCode, resultCode, data);     
    
         if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data){
    
             Uri selectedImage = data.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 picturePath = cursor.getString(columnIndex);
                cursor.close();
                thumbnail = (BitmapFactory.decodeFile(picturePath));
    

    thumbnail is your picture, now play with it!

提交回复
热议问题