Get The File Path To Upload To Server
I am creating a application in which i want to send image to the web Server.For that i am using camera and gallery intent.Now what i want is to get the path of the image which user
Solution 1:
Try this way,hope this will help you to solve your problem.
activity_main.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"><ImageViewandroid:id="@+id/imageView"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/select_photo"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:text="Gallery"/><Buttonandroid:id="@+id/take_photo"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:text="Camera"/></LinearLayout></LinearLayout>
MainActivty.java
publicclassMainActivtyextendsActivity {
privatestaticfinalintCAMERA_REQUEST=1;
privatestaticfinalintRESULT_LOAD_IMAGE=2;
Button camera;
Button gallery;
ImageView imageView;
private String imgPath;
protectedvoidonCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
setContentView(R.layout.launcher);
gallery = (Button) findViewById(R.id.select_photo);
camera = (Button) findViewById(R.id.take_photo);
imageView = (ImageView) findViewById(R.id.imageView);
gallery.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
Intentintent=newIntent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), RESULT_LOAD_IMAGE);
}
});
camera.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
finalIntentintent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
startActivityForResult(intent, CAMERA_REQUEST);
}
});
}
public Uri setImageUri() {
// Store image in dcimFilefile=newFile(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + newDate().getTime() + ".png");
UriimgUri= Uri.fromFile(file);
this.imgPath = file.getAbsolutePath();
return imgUri;
}
public String getImagePath() {
return imgPath;
}
protectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == RESULT_LOAD_IMAGE) {
StringimagePath= getAbsolutePath(data.getData());
imageView.setImageBitmap(decodeFile(imagepath));
// use imagePath to upload image on server
} elseif (requestCode == CAMERA_REQUEST) {
StringimagePath= getImagePath();
imageView.setImageBitmap(decodeFile(imagePath));
// use imagePath to upload image on server
}
}
}
public String getAbsolutePath(Uri uri) {
if(Build.VERSION.SDK_INT >= 19){
Stringid= uri.getLastPathSegment().split(":")[1];
final String[] imageColumns = {MediaStore.Images.Media.DATA };
finalStringimageOrderBy=null;
UritempUri= getUri();
CursorimageCursor= getContentResolver().query(tempUri, imageColumns,
MediaStore.Images.Media._ID + "="+id, null, imageOrderBy);
if (imageCursor.moveToFirst()) {
return imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
}else{
returnnull;
}
}else{
String[] projection = { MediaStore.MediaColumns.DATA };
Cursorcursor= getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
intcolumn_index= cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} elsereturnnull;
}
}
private Uri getUri() {
Stringstate= Environment.getExternalStorageState();
if(!state.equalsIgnoreCase(Environment.MEDIA_MOUNTED))
return MediaStore.Images.Media.INTERNAL_CONTENT_URI;
return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
public Bitmap decodeFile(String path) {
try {
// Decode image size
BitmapFactory.Optionso=newBitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, o);
// The new size we want to scale tofinalintREQUIRED_SIZE=1024;
// Find the correct scale value. It should be the power of 2.intscale=1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Optionso2=newBitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeFile(path, o2);
} catch (Throwable e) {
e.printStackTrace();
}
returnnull;
}
}
Note : please not forget this permission in AndroidManifest.xml
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Solution 2:
Hello You Can Try Below code to get path of selected Image from gallery..
To Open Gallery...
privatestaticint RESULT_LOAD_IMAGE = 1; //variable declared globally
on ButtonClickListener()
Intenti=newIntent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
Activity Result :
if(data != null){
UriselectedImage= data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursorcursor= context.getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
intcolumnIndex= cursor.getColumnIndex(filePathColumn[0]);
StringpicturePath= cursor.getString(columnIndex); //path of image...
cursor.close();
}
Post a Comment for "Get The File Path To Upload To Server"