Choose a Picture : ImageView « UI « Android






Choose a Picture

   

package app.test;

import java.io.FileNotFoundException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class Test extends Activity implements OnClickListener {
  ImageView chosenImageView;
  Button choosePicture;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    chosenImageView = (ImageView) this.findViewById(R.id.ChosenImageView);
    choosePicture = (Button) this.findViewById(R.id.ChoosePictureButton);
    choosePicture.setOnClickListener(this);
  }
  public void onClick(View v) {
    Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(choosePictureIntent, 0);
  }
  protected void onActivityResult(int requestCode, int resultCode,Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    if (resultCode == RESULT_OK) {
      Uri imageFileUri = intent.getData();
      Display currentDisplay = getWindowManager().getDefaultDisplay();
      int dw = currentDisplay.getWidth();
      int dh = currentDisplay.getHeight() / 2 - 100;
      try {
        BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
        bmpFactoryOptions.inJustDecodeBounds = true;
        Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions);
        bmpFactoryOptions.inSampleSize = 2;
        bmpFactoryOptions.inJustDecodeBounds = false;
        bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
                imageFileUri), null, bmpFactoryOptions);
        chosenImageView.setImageBitmap(bmp);
      } catch (FileNotFoundException e) {
        Log.v("ERROR", e.toString());
      }
    }
  }
}
//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Choose Picture" 
    android:id="@+id/ChoosePictureButton"/>
<ImageView android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:id="@+id/ChosenImageView"></ImageView>
</LinearLayout>

   
    
    
  








Related examples in the same category

1.Set ImageView width, height
2.extends ImageView
3.Load image with ImageView
4.Using ImageView within ListActivity
5.Using ImageView to display image and reference Image resource in layout xml file
6.ImageView background
7.Set Layout Parameters, Scale Type, background for ImageView
8.Using ImageView to display Image
9.ImageView click listener
10.Set Image resource for ImageView
11.Set Image Bitmap for ImageView
12.Set image Uri for ImageView
13.Adding Touch Listener to ImageView
14.Demonstrates setting size constraints on android.widget.ImageView
15.Create ImageView
16.Create an image view, given a drawable. you can set the max size of this imageview as well.
17.download images from the Internet and binds those with the provided ImageView.