Wednesday 29 May 2013

choose image from gallery android


choose image from gallery android in my app, the user need to upload their profile from gallery pop up. I write simple project to demonstrate how to code. ^^





 
The XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/btn_Upload"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:onClick="btn_Upload_onClick"
        android:text="Choose Image" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn_Upload"
        android:layout_below="@+id/btn_Upload"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="69dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

MainActivity.java
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
 final static int REQUEST_CODE = 1;
 private String selectedImagePath;
 ImageView img ;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  img = (ImageView) findViewById(R.id.imageView1);
  
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 public void btn_Upload_onClick(View view) {
  Intent intent = new Intent();
  intent.setType("image/*");
  intent.setAction(Intent.ACTION_GET_CONTENT);
  startActivityForResult(Intent.createChooser(intent, "Select Picture"),
    REQUEST_CODE);
 }
 public String getRealPath(Uri uri) {
  String[] projection = { MediaStore.Images.Media.DATA };
  Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
  int column_index = cursor
    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  cursor.moveToFirst();
  return cursor.getString(column_index);
 }
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  try {
   switch (requestCode) {

   case REQUEST_CODE:
    if (resultCode == Activity.RESULT_OK) {
     // data gives you the image uri. Try to convert that to
     // bitmap
     Uri selectedImageUri = data.getData();
     selectedImagePath = getRealPath(selectedImageUri);
     Toast.makeText(this, selectedImagePath, Toast.LENGTH_LONG)
       .show();
     // System.out.println("Image Path : " + selectedImagePath);
     img.setImageURI(selectedImageUri);

     break;
    } else if (resultCode == Activity.RESULT_CANCELED) {
     // Log.e(TAG, "Selecting picture cancelled");
    }
    break;
   }
  } catch (Exception e) {
   // Log.e(TAG, "Exception in onActivityResult : " + e.getMessage());
  }
 }

}

Full Code Project: http://adf.ly/PkdXd

No comments:

Post a Comment