Reputation: 3596
Let me explain my problem.
I am trying to set Facebook profile image in app Imageview some thing like this ..
// Getting Facebook URL image and converting same to bitmap
Bitmap mIcon1;
URL img_value = new URL("http://graph.facebook.com/"+ userProfileID +"/picture?type=square");
BitmapFactory.Options options = new BitmapFactory.Options();
mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream(), null, options);
Then after ..
// I am setting bitmap to imageview .. some thing like ..
if(mIcon1!=null) {
user_picture.setImageBitmap(mIcon1);
}
Up to here it is working great ...
Now I need to save that Facebook Profile image into my DB located at server ...
I am performing that stuff some thing like ..
// Created a method for encoding ..
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;
}
// Now trying to use this method to get encoded BASE 64 image in string ..
String final_image = encodeTobase64(mIcon1);
Now when I try to send this string to my server then at server end I am recieving broken link ... Instead I must say that it's not working ..
I need to perform two of stuff
Looking forward for any suggestion on this. Thanks!
Upvotes: 0
Views: 2092
Reputation: 3596
I got solution of this and hope this will help some one ..
@ Base concept :-- We generally use GET
or POST
method while sending data to server.
1- GET:-- In this method you can only send specific amount of data ..
2- POST :-- In this method you can send huge amount of data ..
Problem was :-- Exact problem was that .. I was using GET
method while sending data to server. Above mentioned concept was know to me . but, some how I did that mistake.
Solution :-- You simply need to send data to server using POST
method instead of GET
.
Complete solution for this in undermentioned :--
// Define your ASYNC TASK like ..
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
new ImageTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else{
new ImageTask().execute();
}
AND
// Now here comes your complete Async Task over here ..
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 {
if(type_of_login.equalsIgnoreCase("facebook")){
img_value = new URL("http://graph.facebook.com/"+ user_id +"/picture?type=square");
System.out.println("Complete URl is:============================= " + img_value);
}else{
img_value = new URL("https://plus.google.com/s2/photos/profile/"+ user_id +"?sz=50");
System.out.println("Complete URl is:============================= " + img_value);
}
//img_value = new URL("http://graph.facebook.com/"+ userProfileID +"/picture?type=square");
BitmapFactory.Options options = new BitmapFactory.Options();
mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream(), null, options);
Log.d("taking", "3" + img_value);
Log.d("taking", "3" + mIcon1);
Log.d("taking", String.valueOf(mIcon1));
ByteArrayOutputStream bao = new ByteArrayOutputStream();
mIcon1.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] ba = bao.toByteArray();
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_pic.setImageBitmap(mIcon1);
get_string_image = encodeTobase64(mIcon1);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
}
}
}
Now finally our dream method ..
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;
}
Now when you need to send data over API then simply execute HttpPost
instead of HttpGet
That's it .. you are good to go with this ..
Upvotes: 1