2016-03-01 40 views

回答

3

试试这个代码....

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setMessage("Pick Image from") 
      .setPositiveButton("Camera", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        //camera intent 
        Intent cameraIntent = new Intent(ConversationActivity.this, CameraActivity.class); 
        cameraIntent.putExtra("EXTRA_CONTACT_JID", contact.getJid()); 
        startActivity(cameraIntent); 
       } 
      }) 
      .setNegativeButton("Gallery", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        Intent intent = new Intent(); 
        // Show only images, no videos or anything else 
        intent.setType("image/*"); 
        intent.setAction(Intent.ACTION_GET_CONTENT); 
        // Always show the chooser (if there are multiple options available) 
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST); 
       } 
      }); 
    AlertDialog alert = builder.create(); 
    alert.show(); 
2
public class ImageGalleryActivity extends Activity { 
    ImageView imageView; 
    private static int RESULT_LOAD_IMAGE = 1; 

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

     imageView = (ImageView) findViewById(R.id.ImageViewId); 
     TextView LoadImage = (TextView) findViewById(R.id.TextViewId); 
     LoadImage.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       Intent loadIntent = new Intent(Intent.ACTION_PICK, 
       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

       startActivityForResult(loadIntent, RESULT_LOAD_IMAGE); 
      } 
     }); 
    } 


    @Override 
    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(); 

      imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
     } 
    } 
}