Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sajmon.example;
- import android.app.Activity;
- import android.content.Intent;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.MediaStore.Images.Media;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- public class Main extends Activity implements View.OnClickListener {
- private static final int PICK_A_PICTURE = 2235;
- protected Button pick;
- protected TextView filePathText;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- init();
- }
- private void init() {
- pick = (Button) findViewById(R.id.pickBtn);
- filePathText = (TextView) findViewById(R.id.imageFilePath);
- pick.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.pickBtn:
- Intent i = new Intent(Intent.ACTION_PICK, Media.INTERNAL_CONTENT_URI);
- startActivityForResult(i, Main.PICK_A_PICTURE);
- break;
- }
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- switch (requestCode) {
- case Main.PICK_A_PICTURE:
- if (resultCode == RESULT_OK) {
- // get data from Intent
- Uri selectedImage = data.getData();
- // column name that returns file path to picked Image
- String filePathColumn = Media.DATA;
- // now create Cursor and getting file path to image from it
- Cursor c = getContentResolver().query(selectedImage, new String[] {filePathColumn}, null, null, null);
- if (c.moveToFirst()) { // if cursor is not empty
- String filePath = c.getString(c.getColumnIndex(filePathColumn));
- filePathText.setText(filePath);
- }
- c.close();
- }
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment