Sajmon

GalleryIntentForLubos

May 13th, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. package com.sajmon.example;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.database.Cursor;
  6. import android.net.Uri;
  7. import android.os.Bundle;
  8. import android.provider.MediaStore.Images.Media;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.TextView;
  12.  
  13. public class Main extends Activity implements View.OnClickListener {
  14.  
  15.     private static final int PICK_A_PICTURE = 2235;
  16.    
  17.     protected Button pick;
  18.     protected TextView filePathText;
  19.    
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.activity_main);
  24.         init();
  25.     }
  26.    
  27.     private void init() {
  28.         pick = (Button) findViewById(R.id.pickBtn);
  29.         filePathText = (TextView) findViewById(R.id.imageFilePath);
  30.         pick.setOnClickListener(this);
  31.     }
  32.  
  33.     @Override
  34.     public void onClick(View v) {
  35.         switch (v.getId()) {
  36.             case R.id.pickBtn:
  37.                 Intent i = new Intent(Intent.ACTION_PICK, Media.INTERNAL_CONTENT_URI);
  38.                 startActivityForResult(i, Main.PICK_A_PICTURE);
  39.             break;
  40.         }
  41.     }
  42.    
  43.     @Override
  44.     public void onActivityResult(int requestCode, int resultCode, Intent data) {
  45.         super.onActivityResult(requestCode, resultCode, data);
  46.         switch (requestCode) {
  47.             case Main.PICK_A_PICTURE:
  48.                 if (resultCode == RESULT_OK) {
  49.                     // get data from Intent
  50.                    
  51.                     Uri selectedImage = data.getData();
  52.                    
  53.                     // column name that returns file path to picked Image
  54.                    
  55.                     String filePathColumn = Media.DATA;
  56.                    
  57.                     // now create Cursor and getting file path to image from it
  58.                    
  59.                     Cursor c = getContentResolver().query(selectedImage, new String[] {filePathColumn}, null, null, null);
  60.                     if (c.moveToFirst()) { // if cursor is not empty
  61.                         String filePath = c.getString(c.getColumnIndex(filePathColumn));
  62.                         filePathText.setText(filePath);
  63.                     }
  64.                     c.close();
  65.                 }
  66.             break;
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment