Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

how to get selected image from gallery in android?

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){
int SELECT_IMAGE=1;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_IMAGE);

I have to email this image, but I don't now how to implement this:

Intent i = new Intent();
i.setType("application/octet-stream"); 
i.setAction(Intent.ACTION_SEND);
i.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ "[email protected]" });
i.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test subj");
i.putExtra(android.content.Intent.EXTRA_TEXT, "corpo mail test");
startActivity(Intent.createChooser(i, "Send email"));

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...