Sending image to web service using its Uri

All your problems with Audio, Video and Images.

Sending image to web service using its Uri

Postby jr83 » Thu Feb 17, 2011 10:51 am

Hi,
I've been looking into this for the past day or so but can not seem to find a solution. I was wondering if there were any tutorials or if someone knew the solution...
Basically I have saved the Uri of images to a sqlite db in the format of:
'content://media/external/images/media/1'

I am trying to then send this image to a web service...but any tutorials I find all deal with 'File' not Uri...
Could someone point me in the right direction?
Thanks,
jr83
jr83
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Sep 16, 2010 5:21 pm

Top

Re: Sending image to web service using its Uri

Postby imbroken » Thu Feb 17, 2011 2:11 pm

I had the same problem. I just created a file in the cache directory and send this file. Not very nice, I would be interested in a better solution as well.


Code: Select all
]
f = new File(context.getCacheDir() + "/uploadImage");
InputStream is = context.getContentResolver().openInputStream(path);

FileOutputStream fos = new FileOutputStream(f, false);
OutputStream os = new BufferedOutputStream(fos);
byte[] buffer = new byte[1024];
int byteRead = 0;

while ((byteRead = is.read(buffer)) != -1) {
    os.write(buffer, 0, byteRead);
}

fos.flush();
imbroken
Senior Developer
Senior Developer
 
Posts: 131
Joined: Thu Jan 06, 2011 6:57 pm
Location: Germnay

Re: Sending image to web service using its Uri

Postby jr83 » Thu Feb 17, 2011 3:01 pm

I've been googling as much as possible to get something sorted. So far I have come up with the following:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String uristringpic = "content://media/external/images/media/1";
  2.                 Uri newuri = Uri.parse(uristringpic);
  3.                
  4.                 String url = "MYURLPATH";
  5.                                 //I originally tried this line but nothing was happening
  6.                                 //File file = new File(newuri);
  7.                                 //When I hard code a path to a specific file the web service will get called and i will receive the email I have set up but the content is blank
  8.                 File file = new File(Environment.getExternalStorageDirectory(),"DCIM/100MSDCF/DSC00004.jpg");           try {
  9.                         HttpClient httpclient = new DefaultHttpClient();
  10.                         HttpPost httppost = new HttpPost(url);
  11.                         InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1);
  12.                         reqEntity.setContentType("binary/octet-stream");
  13.                         reqEntity.setChunked(true);
  14.                         httppost.setEntity(reqEntity);
  15.                         HttpResponse response = httpclient.execute(httppost);
  16.                 } catch (Exception e) {
  17.                         // show error
  18.                 }
  19.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


The web service is receving the following:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. User-Agent :: Apache-HttpClient/UNAVAILABLE (java 1.4)
  2. Host :: dev.origin-partners.com
  3. Expect :: 100-Continue
  4. Transfer-Encoding :: chunked
  5. Connection :: Keep-Alive
  6. Content-Type :: binary/octet-stream
  7.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


but no content.
Any suggestions?
Thanks,
jr83
jr83
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Sep 16, 2010 5:21 pm

Re: Sending image to web service using its Uri

Postby strider2023 » Tue Mar 01, 2011 4:54 pm

Well had the similar problem myself once making a custom email client (not with intents)...the attachments where not sent properly if I used the exact uri like "String uristringpic = "content://media/external/images/media/1";"...I had to split the string and pass "String uristringpic = "/external/images/media/1";" which surprisingly worked :? See if it helps you guys..

P.S. if the above one did not work try.."String uristringpic = "/media/external/images/media/1";" I dont recall it exactly..
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Exabot [Bot] and 11 guests