socialize / socialize-sdk-android

Socialize SDK for Android. An Android social sharing SDK for native apps.
www.getsocialize.com
Apache License 2.0
147 stars 57 forks source link

Uri Problems in com.socialize.util.ImageUtils #20

Closed sromanuk closed 12 years ago

sromanuk commented 12 years ago

There is an issue with scale function in yours ImageUtils class. If URI points to the resource at the web this function just crashed. I think that's because of this line of code:

InputStream is = context.getContentResolver().openInputStream(photoUri);

My application posts to Facebook and Twitter from the external resource were links are alive just for 24 hours, so I can't just use the link added to the message. And I prefer not to save every image on the user's phone. So I wrote this code snippet:

      public Bitmap getBitmapFromURL(String src) {
        Bitmap myBitmap = null;
        HttpURLConnection connection = null;

        try {
            URL url = new URL(src);
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            myBitmap = BitmapFactory.decodeStream(input);
        } catch (IOException e) {
            e.printStackTrace();
            myBitmap = null;
        } finally {
            try {
                connection.disconnect();
            } catch (Exception ignored) {}
        }

        return myBitmap;
    }

And then I'm using it this way:

Bitmap imgBitmap = ImageUtils.getBitmapFromURL(pictureURL);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imgBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();

tweet.setImageData(data);

I know it does not change the ratio of the photo, but your method of finding it out is pretty bad for my situation with external image resource (I can't make user to download photo twice because they could be huge).

Hope this helps.