如何将facebook个人资料图片图片网址转换为base 64字符串,并将图片设置为android中的imageview?

时间:2015-03-18 05:48:17

标签: android facebook bitmap

如何将facebook图片网址转换为基本64字符串...我已使用以下代码将网址转换为位图,但输出为空

public class MainActivity extends ActionBarActivity {
    private ImageView user_image;
    String image= "http://graph.facebook.com/"user-id"/picture";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        user_image = (ImageView)findViewById(R.id.imageView1);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            new ImageTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        } else{
            new ImageTask().execute();

        }

    }

    private class ImageTask extends AsyncTask<Void, Integer, Void> {
        Bitmap mIcon1;

        @Override
        protected Void doInBackground(Void... params) {
                URL img_value = null;
                Log.d("taking", "2");
                try {
                     getBitmapFromURL(image);
                        img_value = new URL("http://graph.facebook.com/"user-id"/picture");
                        System.out.println("Complete URl is:============================= " + img_value);

//                    img_value = new URL("http://graph.facebook.com/"+ userProfileID +"/picture?type=square");
//                        URL url = new URL("http://....");
                        Bitmap image = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
                    Log.d("taking", "3" + img_value);
                    Log.d("Bitmap", "3" + image);
                    Log.d("taking", String.valueOf(mIcon1));
//
//                    ByteArrayOutputStream bao = new ByteArrayOutputStream();
//                    mIcon1.compress(Bitmap.CompressFormat.JPEG, 100, bao);
//                    byte [] ba = bao.toByteArray();
//                   String encoded_image =Base64.encodeToString(ba,Base64.DEFAULT);
//
//                    System.out.println("Encoded image is : ===== " + encoded_image);

                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }catch(Exception e){
                    e.printStackTrace();
                }



            return null;
        }

        @Override
        protected void onPostExecute(Void result) {



        if(mIcon1!=null)
      {
            user_image.setImageBitmap(mIcon1);


       }
        }
    }

    public static String encodeTobase64(Bitmap image)
    {
        Bitmap immagex=image;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);

        Log.e("LOOK", imageEncoded);
        return imageEncoded;
    }

    public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            Log.d("src",src+"");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Log.d("input",input+"");
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            Log.d("mybitmap",myBitmap+"");
            return myBitmap;
        } catch (IOException e) {
            // Log exception
            return null;
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

我得到空位图...请帮助..我已经使用其他网址转换为位图,但它工作但我无法转换给定的url.Appreciate帮助..!

0 个答案:

没有答案