Android upload image from gallery to server
For uploading images to a server from your application you can follow following tutorials:
The above two url will explain you how to upload images from your application to server.
For uploading image from your photo gallery you require the path of that image file and replace the obtained path with /data/file_to_send.mp3
in first url.
To obtain path of the image from the mobile gallery you can follow the following code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button)findViewById(R.id.Button01);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openGallery(SELECT_FILE1);
}
});
}
public void openGallery(int req_code) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select file to upload "), req_code);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
if (requestCode == SELECT_FILE1) {
selectedPath1 = getPath(selectedImageUri);
System.out.println("selectedPath1 : " + selectedPath1);
}
if (requestCode == SELECT_FILE2) {
selectedPath2 = getPath(selectedImageUri);
System.out.println("selectedPath2 : " + selectedPath2);
}
tv.setText("Selected File paths : " + selectedPath1 + "," + selectedPath2);
}
}
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);
}
for downloading images you can do the following code.
ImageView image = (ImageView)findViewById(R.id.image);
if(!ImageUrl.equals("no image")) {
try {
image.setImageDrawable(grabImageFromUrl(ImageUrl));
} catch(Exception e) {
}
}
private Drawable grabImageFromUrl(String url) throws Exception {
return Drawable.createFromStream((InputStream)new URL(url).getContent(), "src");
}
sbnarra
Updated on August 12, 2022Comments
-
sbnarra over 1 year
I am trying to upload an image from the Androids photo gallery to a server. All the communication I have done has been with Object Streams but now I am unsure as to how I would do this. Oh and I have used an Input Stream to download an image where you point directly to the image using an URL. If someone could point me in the right direction would be appreciated.
Thank You
-
Droidman over 11 yearsplease note that data.getData(); will NOT work on Samsung devices. Use a pre-inserted Uri instead
-
Adnan Mulla almost 11 yearsWell it works on latest devices atleast !
-
Parth Dani almost 11 years@AdnanMulla Thanks...for confirming but need to tell u that when i had posted this comment. I had tested on all the device and then only I had placed my comment over here...So if it really helped you please give a up vote so it can also help other
-
blackdog over 9 yearsIf you want to upload file(image), you just need
new File(URI)
. -
Zaartha almost 8 years@blackdog Does not for gallery image. Returns null.