I have Tried lot of examples and watched many videos and done StackOverFlow, but i am not getting what i want.
1] I want to upload image path to server . 2] My php query required image of type FILE, So i cant send string. I need to upload image file path. I know how to send string and decode back to image.But i have no idea how to send image file path and then get back image.
Plz help me with this,I am in big trouble.
Things i have tried :
private static final int PICK_FROM_CAMERA = 1;
private static final int PICK_FROM_FILE = 2;
private String ImgPath = null;
private Uri mImageCaptureUri;
private String encodedImage;
@Override
public void onClick(View v) {
// onRegisterButtonClick();
ImgPath = null;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(
Intent.createChooser(intent, "Complete action using"),
PICK_FROM_FILE);
}
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 0, "From Camera");
menu.add(0, v.getId(), 0, "From Gallery");
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;
if (requestCode == PICK_FROM_FILE) {
mImageCaptureUri = data.getData();
ImgPath = getRealPathFromURI(mImageCaptureUri); // from Gallery
Log.d("image path", ImgPath.toString());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 5;
Bitmap b = BitmapFactory.decodeFile(ImgPath, options);
if (ImgPath == null) {
ImgPath = mImageCaptureUri.getPath(); // from File Manager
}
if (ImgPath != null) {
// ivProfile.setImageBitmap(b);
// rotate(ivProfile, 270);
ExifInterface ei = null;
try {
ei = new ExifInterface(ImgPath);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int orientation = ei.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_180:
rotate(ivProfile, 180);
ivProfile.setImageBitmap(rotateBitmap(b, 180));
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate(ivProfile, 270);
ivProfile.setImageBitmap(rotateBitmap(b, 270));
break;
}
try {
getBase64String(b);
} catch (Exception e) {
e.printStackTrace();
/* Utils.showAlert(this, "Please select local gallery pics.",
false);*/
}
}
} else if (requestCode == PICK_FROM_CAMERA) {
if (ImgPath == null) {
ImgPath = mImageCaptureUri.getPath(); // from File Manager
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 5;
Bitmap b = BitmapFactory.decodeFile(ImgPath, options);
if (ImgPath != null) {
ivProfile.setImageBitmap(b);
// rotate(ivProfile, 270);
try {
getBase64String(b);
} catch (Exception e) {
// Utils.showAlert(mContext, "Out of memory", false);
e.printStackTrace();
}
}
}
}
private Bitmap rotateBitmap(Bitmap b, int orientation) {
// TODO Auto-generated method stub
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_NORMAL:
return b;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-90);
break;
default:
return b;
}
try {
Bitmap bmRotated = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
b.getHeight(), matrix, true);
b.recycle();
return bmRotated;
} catch (OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}
@SuppressWarnings("deprecation")
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
if (cursor == null)
return null;
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "From Camera") {
ImgPath = null;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),
"tmp_avatar_" + String.valueOf(System.currentTimeMillis())
+ ".jpg");
mImageCaptureUri = Uri.fromFile(file);
try {
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
mImageCaptureUri);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (Exception e) {
e.printStackTrace();
}
} else if (item.getTitle() == "From Gallery") {
ImgPath = null;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(
Intent.createChooser(intent, "Complete action using"),
PICK_FROM_FILE);
} else {
return false;
}
return true;
}
public String getBase64String(Bitmap path) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
path.compress(Bitmap.CompressFormat.JPEG, 50, baos);
byte[] biteArray = baos.toByteArray();
encodedImage = Base64.encodeToString(biteArray, Base64.DEFAULT);
return encodedImage;
}
private void rotate(View iv, float degree) {
final RotateAnimation rotateAnim = new RotateAnimation(0.0f, degree,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotateAnim.setDuration(0);
rotateAnim.setFillAfter(true);
iv.startAnimation(rotateAnim);
}
and then i have set encodedImage to parameters and send it to server,But it is not working