Powered By Blogger

Wednesday 6 March 2013

Select image from Gallery



private PopupWindow pv = null;
ImageButton add_img;
add_img.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater li = (LayoutInflater) CreateAccount.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = li.inflate(R.layout.popup_window_layout,
(ViewGroup) findViewById(R.id.popup_layout));
cancle = (Button) popupView.findViewById(R.id.popup_cancle);
cancle.setTypeface(tf1);
cancle.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
pv.dismiss();

}
});

uploadPhoto = (Button) popupView
.findViewById(R.id.popup_upload_photo);
uploadPhoto.setTypeface(tf1);
uploadPhoto.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("uploadPhoto clicked");
Intent intent = new Intent();
intent.setType("image/*");

intent.setAction(Intent.ACTION_GET_CONTENT);
// setResult(Activity.RESULT_OK);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
1111);
pv.dismiss();

}
});


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode == RESULT_OK) {

               if (requestCode == 1111) {
System.out
.println("After Result 1111..........................................");

System.out.println("With in Take Image");
Uri imageUri = data.getData();
System.out.println(imageUri);
Cursor c = getContentResolver().query(imageUri, null, null,
null, null);
c.moveToFirst();
String imagePath = c.getString(c
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
System.out
.println("Before Setting Image:::::::::::::::::::::::::::::::::::::::::"
+ imagePath);
set_Image(imagePath);

try {
io = new FileInputStream(new File(imagePath));
System.out
.println("IO got set:::::::::::::::::::::::::::::::::::::::");
} catch (Exception e) {
e.printStackTrace();
}
image = imagePath;

}
}
super.onActivityResult(requestCode, resultCode, data);



}




private void set_Image(String imagePath) {
BitmapFactory.Options opt = null;

try {
System.out
.println("Image Path::::::::::::::::::::in Set Image::::::::::::::::::::"
+ imagePath);
FileInputStream in = new FileInputStream(imagePath);
System.out.println("1..................");
opt = new BitmapFactory.Options();
opt.inTempStorage = new byte[16 * 1024];
opt.inSampleSize = 4;
opt.outWidth = 640;
opt.outHeight = 480;
Bitmap imageBitmap = BitmapFactory
.decodeStream(in, new Rect(), opt);
Bitmap map = this.scaleDown(imageBitmap, 10, true);
BitmapDrawable bmd = new BitmapDrawable(map);

ByteArrayOutputStream bao = new ByteArrayOutputStream();

map.compress(Bitmap.CompressFormat.PNG, 90, bao);

ba = bao.toByteArray();

imagedata = Base64.encodeBytes(ba);
System.out.println("the image data is............." + imagedata);

add_img.setImageDrawable(bmd);
if (in != null) {
in.close();
}

} catch (OutOfMemoryError e) {
opt.inTempStorage = new byte[50 * 1024];
System.out.println(e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


XML



<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
android:id="@+id/popup_layout"
android:background="#404040">
<!-- <Button
android:id="@+id/popup_take_photo"
android:layout_gravity="center"
android:layout_marginTop="20dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn_large_light_grey_key" android:text="Take Photo" android:textColor="#000000" android:textSize="14dip" android:textStyle="bold"/> -->
<Button
android:id="@+id/popup_upload_photo"
android:layout_gravity="center"
android:layout_marginTop="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Upload Photo" android:textColor="#000000" android:textSize="14dip" android:textStyle="bold"
android:background="@drawable/btn_large_light_grey_key"/>
<Button
android:id="@+id/popup_cancle"
android:layout_gravity="center"
android:layout_marginTop="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" android:textColor="#000000"
android:textSize="14dip" android:textStyle="bold"
android:background="@drawable/btln_large_dark_gray"/>
</LinearLayout>




No comments:

Post a Comment