Create An XML page (UI) With following controls.
TextView, Button, ImageView,
upload_photo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/sd_background"
android:orientation="vertical" >
<LinearLayout android:background="@drawable/sd_topbar" android:layout_height="44dip" android:layout_width="wrap_content">
<Button
android:layout_margin="4dip"
android:id="@+id/add_pa_back_btn"
android:layout_width="100dip"
android:layout_marginLeft="10dip"
android:layout_height="40dip"
android:textSize="15dip"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:background="@drawable/sd_btn_onclick_over"
android:text="Back" />
<TextView
android:id="@+id/add_pa_username_txt1"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:textSize="18dip"
android:textColor="#F88017"
android:text="Upload Photo"
android:textStyle="bold"
android:layout_marginLeft="180dip"/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="80dip"
android:layout_marginLeft="200dip"
android:layout_height="40dip"
android:background="@drawable/sd_logo" />
</LinearLayout>
<ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/inner" android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:layout_marginTop="5dip" android:layout_marginBottom="5dip">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/transparent"
android:orientation="horizontal" >
<TextView
android:id="@+id/uplod_TV_patimage"
android:layout_width="120dip"
android:layout_marginLeft="20dip"
style="@style/FixAppointMentDettextView"
android:layout_height="wrap_content"
android:text="Image" android:layout_marginTop="40dip"/>
<ImageView
android:id="@+id/add_pa_loadedphoto"
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_marginLeft="25dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" android:layout_alignParentLeft="false" android:layout_alignParentTop="false" android:layout_marginTop="20dip"/>
<Button
android:id="@+id/photo_uploadbutton"
android:layout_width="70dp"
android:layout_height="50dp"
android:text="Go"
android:textColor="#FFFFFF"
android:layout_marginLeft="25dip"
android:textStyle="bold"
android:background="@drawable/sd_btn_onclick_over" android:layout_marginTop="40dip"/>
<Button
android:id="@+id/photo_Realuploadbutton"
android:layout_width="70dp"
android:layout_marginLeft="25dip"
android:background="@drawable/sd_btn_onclick_over"
android:layout_height="50dp"
android:text="Upload"
android:textColor="#FFFFFF"
android:textStyle="bold" android:layout_marginTop="40dip"/>
</LinearLayout>
</ScrollView>
</LinearLayout>


Use The following JAVA codes to select the image from the phone gallery and Upload
upload_photo.JAVA
public class upload_photo extends Activity {
TextView uplod_TV_patimage;
Button photo_uploadbutton;
Button add_pa_back_btn;
ImageView add_pa_loadedphoto;
Bundle fx_bundle;
Button photo_Realuploadbutton;
String cKey;
String toastValue="";
boolean result2;
private String path = "";
private static final int SELECT_PICTURE = 1;
private String selectedImagePath = "";
private static final int CAMERA_PIC_REQUEST2 = 1338;
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.upload_photo);
fx_bundle = getIntent().getExtras();
cKey = fx_bundle.getString("pa_id");
uplod_TV_patimage = (TextView) findViewById(R.id.uplod_TV_patimage);
photo_uploadbutton = (Button) findViewById(R.id.photo_uploadbutton);
add_pa_loadedphoto = (ImageView) findViewById(R.id.add_pa_loadedphoto);
photo_Realuploadbutton=(Button) findViewById(R.id.photo_Realuploadbutton);
photo_uploadbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);
}
});
photo_Realuploadbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
result2 = uploadFile(cKey);
Log.i(SmartDOnlineWebserviceHelper.LOG_TAG, "resulttt:::::::::::: " +result2);
System.out.println("photooooooooooooooooooo"+cKey);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri uriimage = data.getData();
path = getImagePath(uriimage);
File file = new File(path);
Bitmap bitmap = decodeFile(file);
add_pa_loadedphoto.setImageBitmap(bitmap);
if (requestCode == CAMERA_PIC_REQUEST2) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
// Uri uriimage1 = data.getData();
// pvtimage.setImageURI(uriimage1);
add_pa_loadedphoto.setImageBitmap(thumbnail);
final ContentResolver cr = getContentResolver();
final String[] p1 = new String[] {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATE_TAKEN };
Cursor c1 = cr.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null,
null, p1[1] + " DESC");
if (c1.moveToFirst()) {
String uristringpic = "content://media/external/images/media/"
+ c1.getInt(0);
Uri newuri = Uri.parse(uristringpic);
Log.i("TAG", "newuri " + newuri);
selectedImagePath = getPath(newuri);
//uploadFile(cKey);
}
c1.close();
}
}
}
public String getImagePath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
private Bitmap decodeFile(File f) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
protected boolean uploadFile(String cKey) {
File uploadFile;
FileInputStream fis;
photouploader htfu;
try {
if(!path.equalsIgnoreCase("")) {
//Have a private image
uploadFile = new File(path);
fis = (FileInputStream)this.getContentResolver().openInputStream(Uri.fromFile(uploadFile));
htfu = new photouploader (SmartDOnlineWebserviceHelper.UPLOADIMAGE+ cKey, uploadFile.getName());
htfu.doStart(fis);
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}
define the server and image path URL to the Webservicehelper's public class
Webservicehelper.JAVA
public static final String UPLOADIMAGE = SERVER_NAME
+ "/smadws/uploadimages.aspx?pid=";
(the url is based in the wbservice provided)
NOTE : the above method is to update the image by passing the the users key, For normal update please avoid the data passed through Bundle values in the above java code, "ie: cKey"
No comments:
Post a Comment