I am creating app where i need to add uploading image from gallery to server,I already aske this but did not get any answer,here it is How to upload image from gallary to server? problem is whenever I select from gallery and trying to send,it shows source file not exists,can any one help?
Asked
Active
Viewed 1,790 times
1
-
can u able to get the path/uri of the gallery file..? – Rajesh Mikkilineni Dec 18 '14 at 09:51
-
yes i added one image in my emulator gallery service_lifecycle,but when i add apk in my cellphone and select picture from gallery it not uploading – Dec 18 '14 at 09:53
-
what kind of error your getting..? – Rajesh Mikkilineni Dec 18 '14 at 09:54
-
source file not exists – Dec 18 '14 at 09:57
-
check this answer in this link http://stackoverflow.com/questions/21781831/android-upload-of-file-to-server – Rajesh Mikkilineni Dec 18 '14 at 10:39
-
@RajeshMikkilineni i want to send image which i get from gallery to sever – Dec 18 '14 at 11:02
1 Answers
1
Start intent to select image file from gallery...
startActivityForResult(new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI),54);
On Activity Result get file URI...
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode != RESULT_CANCELED) {
try {
if (requestCode == 54 && resultCode != 0) {
if (intent != null) {
Uri selectedImage = intent.getData();
} else {
Toast.makeText(getApplicationContext(),
"Could not load image",
Toast.LENGTH_SHORT).show();
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}
}
Get file bytes from URI...
private byte[] GetFileBytes(Uri uri)
{
ByteArrayOutputStream bos = new ByteArrayOutputStream(200000);
InputStream imageStream;
try {
imageStream = getContentResolver().openInputStream(uri);
byte[] buffer;
buffer = new byte[100000];
while ((imageStream.read(buffer)) != -1) {
bos.write(buffer);
}
} catch (Throwable e) {
e.printStackTrace();
}
return bos.toByteArray();
}
Get MultipartEntity from byte array...
private MultipartEntity getEntity(byte[] bytes)
{
MultipartEntity entity = new MultipartEntity();
InputStream in = new ByteArrayInputStream(bytes);
ContentBody bin = new InputStreamBody(in, "Image_" + c.getTimeInMillis() + ".jpg");
entity.addPart("image_query_string_variable_name", bin);
}
MultipartEntity is a class of httpmime. You can find the JAR easily by Googling. You will need to add this jar file to your project from project properties.
Post to server...
private void PostData(MultipartEntity entity)
{
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 20000);
HttpConnectionParams.setSoTimeout(httpParameters, 20000);
DefaultHttpClient client = new DefaultHttpClient(httpParameters);
HttpPost request = new HttpPost("http://www.you_server_url.com/someFileName.php");
request.setEntity(entity);
HttpResponse response = null;
response = client.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
//
// Process the returned result from server...
//
}
More Edit:
byte[] fileBytes = null;
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode != RESULT_CANCELED) {
try {
if (requestCode == 54 && resultCode != 0) {
if (intent != null) {
Uri selectedImage = intent.getData();
fileBytes = GetFileBytes(selectedImage);
} else {
fileBytes = null;
Toast.makeText(getApplicationContext(),
"Could not load image",
Toast.LENGTH_SHORT).show();
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}
}
// when button is clicked...
public void onClick(View v)
{
if(fileBytes != null)
{
MultipartEntity entity = getEntity(fileBytes);
PostData(entity);
}
}
Good Luck. :)

Hemendra Sharma
- 1,063
- 9
- 21
-
-
-
-
-
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/67219/discussion-between-eddie-and-hemendra-sharma). – Dec 18 '14 at 11:59