Sunday, 16 June 2013

Get Path for Selected Image

Demo:



1)MainActivity.java:

package com.venky.getpathfromselectedimage;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends Activity {

      Button btnSelectImage;
      EditText edtPath;
      ImageView iv;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            btnSelectImage = (Button) findViewById(R.id.button1);
            edtPath = (EditText) findViewById(R.id.editText1);
            iv = (ImageView) findViewById(R.id.imageView1);

            btnSelectImage.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View arg0) {
                        // TODO Auto-generated method stub

                        Intent browseIntent = new Intent(Intent.ACTION_PICK);
                        browseIntent.setType("image/*");
                        startActivityForResult(browseIntent, 1);
                  }
            });
      }

      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == 1) {

                  try {

                        Uri selectedImage = data.getData();
                        InputStream is = getContentResolver().openInputStream(
                                    selectedImage);
                        Bitmap bitmap = BitmapFactory.decodeStream(is);
                        iv.setImageBitmap(bitmap);

                        File imgFile = new File(getRealPathFromURI(selectedImage));

                        edtPath.setText(imgFile.toString());

                  } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                  }
            }
      }

      public String getRealPathFromURI(Uri uri) {

            Cursor cursor = getContentResolver().query(uri, null, null, null, null);
            if (cursor == null)

                  return uri.getPath();

            else {
                  cursor.moveToFirst();
                  int idx = cursor
                              .getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                  return cursor.getString(idx);
            }
      }

}

2)activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Browse Image" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp" >
    </EditText>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp" />

</LinearLayout>
 

3)Download this code Click Here

1 comment: