Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2012
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. Android get only image from gallery
  2. @Override
  3. public void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.main);
  6. Intent intent = new Intent();
  7. intent.setType("image/*");
  8. intent.setAction(Intent.ACTION_GET_CONTENT);
  9. startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
  10. }
  11.  
  12. @Override
  13. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  14. super.onActivityResult(requestCode, resultCode, data);
  15.  
  16.  
  17. if (resultCode == RESULT_OK) {
  18.  
  19.  
  20. switch(requestCode){
  21.  
  22. case SELECT_PICTURE:
  23. Uri selectedImageUri = data.getData();
  24.  
  25.  
  26. break;
  27. }
  28. }
  29.  
  30. ....
  31. Intent intent = new Intent(Intent.ACTION_PICK);
  32. intent.setType("image/*");
  33. startActivityForResult(intent, SELECT_PICTURE);
  34. ....
  35.  
  36. Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  37. startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
  38.  
  39. startActivityForResult(intent, SELECT_PICTURE);
  40.  
  41. public void ChoosePicture(View v) {
  42. Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
  43. photoPickerIntent.setType("image/*");
  44. startActivityForResult(photoPickerIntent, 1);
  45. }
  46.  
  47. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  48. super.onActivityResult(requestCode, resultCode, data);
  49. switch (requestCode) {
  50. case 1:
  51. {
  52. if (resultCode == RESULT_OK)
  53. {
  54. Uri photoUri = data.getData();
  55. if (photoUri != null)
  56. {
  57. try {
  58. String[] filePathColumn = {MediaStore.Images.Media.DATA};
  59. Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null);
  60. cursor.moveToFirst();
  61. int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
  62. String filePath = cursor.getString(columnIndex);
  63. cursor.close();
  64. bMap_image = BitmapFactory.decodeFile(filePath);
  65. ImageView img = (ImageView) findViewById(R.id.gallery1);
  66. img.setImageBitmap(bMap_image);
  67.  
  68.  
  69. }catch(Exception e)
  70. {}
  71. }
  72. }// resultCode
  73. }// case 1
  74. }// switch, request code
  75. }// public void onActivityResult
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement