开发者

android: webservice image replace with images in Local folder

开发者 https://www.devze.com 2023-04-08 11:10 出处:网络
I am into final stage of chat application in android. I am facing few issues while working with images and web service.

I am into final stage of chat application in android.

I am facing few issues while working with images and web service.

So when i select the image and sent that image to web service i get URL from service. How can i convert that url into image that is in my local folder. I am confused how can i make this worki开发者_运维知识库ng.

I want to display the selected image in list view along with message returned from web service.

Please guide me in this issue. Is there another option to work this issue.

Thanks.


Use this method:

public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url
            .openConnection();
            connection.setDoInput(true);
            connection.connect();
            connection.setReadTimeout(120000);
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

Here You need to pass the URL of the image to the function and you can set the image as setImageBitmap function.


use this method to convert url into image Bitmap bi=convertImage(url);, mention your image sizes in this line

bit=Bitmap.createScaledBitmap(bm,120, 120, true);

public Bitmap convertImage(String url)
         {

            URL aURL = null;
            try 
           {
            final String imageUrl =url.replaceAll(" ","%20");
            Log.e("Image Url",imageUrl);
            aURL = new URL(imageUrl);
            URLConnection conn = aURL.openConnection();
            InputStream is = conn.getInputStream(); 

            BufferedInputStream bis = new BufferedInputStream(is); 
            Bitmap bm = BitmapFactory.decodeStream(new PatchInputStream(bis)); 
            if(bm==null)
            {}
            else
              bit=Bitmap.createScaledBitmap(bm,120, 120, true);

            return bm;

           } 


            catch (IOException e) 
            {
               Log.e("error in bitmap",e.getMessage());
              return null;
           }


          }

Set returned bm(bitmap) to your ImageView imageview.setImageBitmap(bi) feel free to ask doubts

0

精彩评论

暂无评论...
验证码 换一张
取 消