Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 13th, 2012  |  syntax: None  |  size: 4.79 KB  |  views: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class FileDialogBoxActivity extends Activity {
  2.  final int SELECT_PHOTO = 0;
  3.  ImageView image;
  4.  
  5.  /** Called when the activity is first created. */
  6.  @Override
  7.  public void onCreate(Bundle savedInstanceState) {
  8.  super.onCreate(savedInstanceState);
  9.  setContentView(R.layout.main);
  10.  image = (ImageView) findViewById(R.id.imageView2);
  11.  Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
  12.  photoPickerIntent.setType("image/*");
  13.  
  14.  startActivityForResult(photoPickerIntent, SELECT_PHOTO);
  15.  }
  16. @Override
  17. protected void onActivityResult(int requestCode, int resultCode, Intent      
  18. imageReturnedIntent) {
  19. super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
  20.  
  21. switch(requestCode) {
  22. case SELECT_PHOTO:
  23. if(resultCode == RESULT_OK){
  24. Uri selectedImage = imageReturnedIntent.getData();
  25. InputStream imageStream=null;
  26. try {
  27. imageStream = getContentResolver().openInputStream(selectedImage);
  28.  } catch (FileNotFoundException e) {
  29.  // TODO Auto-generated catch block
  30. e.printStackTrace();
  31. }
  32.  Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
  33.  image.setImageBitmap(yourSelectedImage);
  34.  }
  35.  }
  36.  }
  37.  
  38.  }
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. import java.io.ByteArrayOutputStream;
  55.   import java.io.FileNotFoundException;
  56.   import java.io.InputStream;
  57.   import java.util.ArrayList;
  58.   import java.util.List;
  59.  
  60.   import org.apache.http.NameValuePair;
  61.  import org.apache.http.message.BasicNameValuePair;
  62.  import android.app.Activity;
  63.  import android.content.Intent;
  64.  import android.graphics.Bitmap;
  65.  import android.graphics.BitmapFactory;
  66.  import android.net.Uri;
  67.  import android.os.Bundle;
  68.  import android.util.Log;
  69.  import android.view.View;
  70.  import android.widget.Button;
  71.  import android.widget.ListView;
  72.  
  73.   public class SQLiteDemoActivity extends Activity {
  74.     //private static final int SELECT_PICTURE = 1;
  75.     private static final int SELECT_PHOTO = 100;
  76.  
  77.     public static InputStream  imgstrem;
  78.  
  79.  
  80.  
  81.     public static InputStream SELECT_PICTURE;
  82.    ArrayList<Contact> imageArry = new ArrayList<Contact>();
  83.   ContactImageAdapter adapter;
  84.   Button BrowseButton;
  85.  
  86.    Bitmap image;
  87.  
  88.  /** Called when the activity is first created. */
  89.  @Override
  90.  public void onCreate(Bundle savedInstanceState) {
  91.  super.onCreate(savedInstanceState);
  92.  setContentView(R.layout.main);
  93.  
  94.  DataBaseHandler db = new DataBaseHandler(this);
  95.  
  96.  SELECT_PICTURE = imgstrem;
  97.  
  98.   Bitmap image = BitmapFactory.decodeStream(SELECT_PICTURE);
  99.  
  100.  
  101.   BrowseButton=(Button)findViewById(R.id.BrowseButton);
  102.  
  103.  
  104.  
  105.   BrowseButton.setOnClickListener(new View.OnClickListener() {
  106.  
  107.    @Override
  108.   public void onClick(View v) {
  109.     // TODO Auto-generated method stub
  110.      // in onCreate or any event where your want the user to
  111.     // select a file
  112.  /*     Intent intent = new Intent();
  113.     intent.setType("image/*");
  114.     intent.setAction(Intent.ACTION_GET_CONTENT);
  115.     startActivityForResult(Intent.createChooser(intent,
  116.             "Select Picture"), SELECT_PICTURE);
  117.  */
  118.  
  119.  
  120.  // Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
  121.  // photoPickerIntent.setType("image/*");
  122.  // startActivityForResult(photoPickerIntent, SELECT_PHOTO);  
  123.  
  124.  
  125.                  // Taking image from phone gallery
  126.     Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
  127.     photoPickerIntent.setType("image/*");
  128.  
  129.     startActivityForResult(photoPickerIntent, SELECT_PHOTO);
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  }
  137.   });
  138.  
  139.  
  140.    //Bitmap image = BitmapFactory.decodeStream(imageStream);
  141.  
  142.  
  143.  
  144.  
  145.  //convert bitmap to byte
  146.   ByteArrayOutputStream stream = new ByteArrayOutputStream();
  147.    image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
  148.   byte imageInByte[] = stream.toByteArray();
  149.  /**
  150.   * CRUD Operations
  151.  * */
  152. //Inserting Contacts
  153. Log.d("Insert: ", "Inserting ..");
  154.  db.addContact(new Contact("FaceBook", imageInByte));
  155.  //display main List view bcard and contact name
  156.  
  157.  //Reading all contacts from database
  158.  List<Contact> contacts = db.getAllContacts();
  159.  for (Contact cn : contacts) {
  160.  String log = "ID:" + cn.getID() + " Name: " + cn.getName()
  161.   + " ,Image: " + cn.getImage();
  162.  
  163. //Writing Contacts to log
  164.  Log.d("Result: ", log);
  165.  //add contacts data in arrayList
  166.  imageArry.add(cn);
  167.  
  168.  }
  169.  adapter = new ContactImageAdapter(this, R.layout.screen_list,
  170.  imageArry);
  171.  ListView dataList = (ListView) findViewById(R.id.list);
  172.  dataList.setAdapter(adapter);
  173.  }
  174.  
  175.  @Override
  176.  protected void onActivityResult(int requestCode, int resultCode, Intent        
  177.   imageReturnedIntent) {
  178.   super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
  179.  
  180.  switch(requestCode) {
  181.  case SELECT_PHOTO:
  182.  if(resultCode == RESULT_OK){
  183.  Uri selectedImage = imageReturnedIntent.getData();
  184.  InputStream imageStream=null;
  185.  try {
  186.  imageStream = getContentResolver().openInputStream(selectedImage);
  187.   imgstrem = imageStream;
  188.  } catch (FileNotFoundException e) {
  189.  // TODO Auto-generated catch block
  190.  e.printStackTrace();
  191. }
  192. image = BitmapFactory.decodeStream(imageStream);
  193. //image.setImageBitmap(yourSelectedImage);
  194. }
  195. }
  196. }
  197. }