1

I want to convert bitmap to URI conversion I am using google image search API from this API I got image URL from this URL I converted it to a bitmap then I want to convert from this bitmap to URI using this sample but I get a null value.

private class GetXMLTask extends AsyncTask<String, Void, Bitmap> { @Override protected Bitmap doInBackground(String... urls) { Bitmap map = null; for (String url : urls) { map = downloadImage(url); } return map; } // Sets the Bitmap returned by doInBackground @Override protected void onPostExecute(Bitmap result) { imagQest.setImageBitmap(result); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); result.compress(Bitmap.CompressFormat.JPEG, 100, bytes); String path = Images.Media.insertImage(ctx.getContentResolver(), result, "Title", null); LivLog.info(this.getClass(), "bit map conversion is" + result + path); } // Creates Bitmap from InputStream and returns it private Bitmap downloadImage(String url) { Bitmap bitmap = null; InputStream stream = null; BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inSampleSize = 1; try { stream = getHttpConnection(url); bitmap = BitmapFactory.decodeStream(stream, null, bmOptions); // stream.close(); } catch (IOException e1) { e1.printStackTrace(); } return bitmap; } // Makes HttpURLConnection and returns InputStream private InputStream getHttpConnection(String urlString) throws IOException { InputStream stream = null; URL url = new URL(urlString); URLConnection connection = url.openConnection(); try { HttpURLConnection httpConnection = (HttpURLConnection) connection; httpConnection.setRequestMethod("GET"); httpConnection.connect(); if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { stream = httpConnection.getInputStream(); } } catch (Exception ex) { ex.printStackTrace(); } return stream; } } 

How to resolve this?

1
  • @BirajZalavadia my problem is i uploading images to amazon server using amazon api while i am using google image search api to search images from net after using this api i got one iamge url from this image url i dowload a images then i got a bitmap form this bitmap i want to convert uri for amazon iamge uploading while i coverting bitmap to uri above sample code i got null value. Commented May 21, 2014 at 6:31

2 Answers 2

5

Here is how you'd convert Bitmap to Uri:

private Uri getImageUri(Context context, Bitmap inImage) { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), inImage, "Title", null); return Uri.parse(path); } 

Pass Bitmap while calling this method:

getImageUri(this,bitmap); 
Sign up to request clarification or add additional context in comments.

1 Comment

this is deprecated and returns null now .. not working for any other solution?
4

try below code:-

public Uri getImageUri(Context inContext, Bitmap inImage) { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null); return Uri.parse(path); } 

for more info see below link:-

http://colinyeoh.wordpress.com/2012/05/18/android-getting-image-uri-from-bitmap/

4 Comments

@Golup using above that code this line i got error fileString path = Images.Media.insertImage(ctx.getContentResolver(), result, "Title", null); not found
Isn't working on 6.0. Also, what is the use of bytes BAOS??
did you fix it ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.