Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FileDialogBoxActivity extends Activity {
- final int SELECT_PHOTO = 0;
- ImageView image;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- image = (ImageView) findViewById(R.id.imageView2);
- Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
- photoPickerIntent.setType("image/*");
- startActivityForResult(photoPickerIntent, SELECT_PHOTO);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent
- imageReturnedIntent) {
- super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
- switch(requestCode) {
- case SELECT_PHOTO:
- if(resultCode == RESULT_OK){
- Uri selectedImage = imageReturnedIntent.getData();
- InputStream imageStream=null;
- try {
- imageStream = getContentResolver().openInputStream(selectedImage);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
- image.setImageBitmap(yourSelectedImage);
- }
- }
- }
- }
- import java.io.ByteArrayOutputStream;
- import java.io.FileNotFoundException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.http.NameValuePair;
- import org.apache.http.message.BasicNameValuePair;
- 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.View;
- import android.widget.Button;
- import android.widget.ListView;
- public class SQLiteDemoActivity extends Activity {
- //private static final int SELECT_PICTURE = 1;
- private static final int SELECT_PHOTO = 100;
- public static InputStream imgstrem;
- public static InputStream SELECT_PICTURE;
- ArrayList<Contact> imageArry = new ArrayList<Contact>();
- ContactImageAdapter adapter;
- Button BrowseButton;
- Bitmap image;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- DataBaseHandler db = new DataBaseHandler(this);
- SELECT_PICTURE = imgstrem;
- Bitmap image = BitmapFactory.decodeStream(SELECT_PICTURE);
- BrowseButton=(Button)findViewById(R.id.BrowseButton);
- BrowseButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- // in onCreate or any event where your want the user to
- // select a file
- /* Intent intent = new Intent();
- intent.setType("image/*");
- intent.setAction(Intent.ACTION_GET_CONTENT);
- startActivityForResult(Intent.createChooser(intent,
- "Select Picture"), SELECT_PICTURE);
- */
- // Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
- // photoPickerIntent.setType("image/*");
- // startActivityForResult(photoPickerIntent, SELECT_PHOTO);
- // Taking image from phone gallery
- Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
- photoPickerIntent.setType("image/*");
- startActivityForResult(photoPickerIntent, SELECT_PHOTO);
- }
- });
- //Bitmap image = BitmapFactory.decodeStream(imageStream);
- //convert bitmap to byte
- ByteArrayOutputStream stream = new ByteArrayOutputStream();
- image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
- byte imageInByte[] = stream.toByteArray();
- /**
- * CRUD Operations
- * */
- //Inserting Contacts
- Log.d("Insert: ", "Inserting ..");
- db.addContact(new Contact("FaceBook", imageInByte));
- //display main List view bcard and contact name
- //Reading all contacts from database
- List<Contact> contacts = db.getAllContacts();
- for (Contact cn : contacts) {
- String log = "ID:" + cn.getID() + " Name: " + cn.getName()
- + " ,Image: " + cn.getImage();
- //Writing Contacts to log
- Log.d("Result: ", log);
- //add contacts data in arrayList
- imageArry.add(cn);
- }
- adapter = new ContactImageAdapter(this, R.layout.screen_list,
- imageArry);
- ListView dataList = (ListView) findViewById(R.id.list);
- dataList.setAdapter(adapter);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent
- imageReturnedIntent) {
- super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
- switch(requestCode) {
- case SELECT_PHOTO:
- if(resultCode == RESULT_OK){
- Uri selectedImage = imageReturnedIntent.getData();
- InputStream imageStream=null;
- try {
- imageStream = getContentResolver().openInputStream(selectedImage);
- imgstrem = imageStream;
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- image = BitmapFactory.decodeStream(imageStream);
- //image.setImageBitmap(yourSelectedImage);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement