I use the following code do get an image from a regular image url:
try
{
url = new URL("http://example.com/test.jpg");
final Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
final ImageView imageView=(ImageView)findViewById(R.id.imageView2);
MainActivity.this.runOnUiThread(new Runnable()
{
#Override
public void run()
{
imageView.setImageBitmap(bmp);
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
However, I want to download images that are displayed by html or php pages.
url = new URL("http://example.com/showimage.php");
If I use this, I get the following message:
SkImageDecoder::Factory returned null
How should I modify my code?
I recommend you to look at this library: http://jsoup.org/
This seems to be the solution when the page is redirected: http://blog.kosev.net/2011/01/follow-302-redirects-with.html
Related
I am trying to convert url to bitmap and then set that bitmap to background as wallpaper. And all this process is getting done in background with the use of worker class in android. But I am getting no protocol error. I am fetching any one random wallpaper link from firebase then I am trying to convert it into bitmap by
try {
URL url = new URL(image_url);
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
myWallpaperManager.setBitmap(image,null,false,WallpaperManager.FLAG_SYSTEM);
} catch(IOException e) {
Log.e("tag102",e.getMessage());
}
But this method gave me this error in logcat
2022-07-08 03:15:07.544 25513-25752/com.nightowl.stylo E/tag102: no protocol:
But when I put hardcoded string (showed in below method) in url method parameter then it return bitmap without any error. and wallpaper also gets changed
try {
URL url = new URL("https://images.pexels.com/photos/6336035/pexels-photo-6336035.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=1200&w=800");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
myWallpaperManager.setBitmap(image,null,false,WallpaperManager.FLAG_SYSTEM);
} catch(IOException e) {
Log.e("tag102",e.getMessage());
}
But i want random string to set and get me bitmap from that. So how i can achieve that? I also try to encode url by
try {
encodedURL = URLEncoder.encode(image_url, "UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e("tag3",e.toString());
}
I am using three kind of url in this project
https://images.pexels.com/photos/6336035/pexels-photo-6336035.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=1200&w=800
https://cdn.pixabay.com/photo/2020/11/27/22/07/naruto-5783102_960_720.png
https://images.unsplash.com/photo-1641414315243-196e7382c32d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1178&q=80
Any help will be appreciated.
How do i call a link that is stored under
R.strings.myLink , in the below line
URL url = new URL("https://www.myLink.com");
getString(R.string.myLink)
If above shows error, then use below:
context.getResources().getString(R.string.myLink)
use this code
try {
URL url = new URL(getResources().getString(R.string.urllink));
} catch (MalformedURLException e) {
e.printStackTrace();
}
It is possible to post to a Facebook wall a message with image (not an image link, but image data)?
I did not find this possibility either in http://developers.facebook.com/docs/reference/api/post/ or in https://developers.facebook.com/docs/guides/attachments/.
And I was ready to put up with impossibility of doing it, but I came across documentation for SLComposeViewController class introduced in iOS 6.0 (http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Reference/SLComposeViewController_Class/Reference/Reference.html).
This class has a method - (BOOL)addImage:(UIImage *)image that does exactly what I need.
I program for Android and hence I cannot use it. But obviously this method must use facebook API. But I cannot find it: everything related to image posting requires url, not data.
So, is is possible in Android to post to a Facebook wall a message with image data?
EDIT: I posted the answer almost with your comment. You can use something like this to cast you Image from assets into a Bitmap.
InputStream bitmap = null;
try {
bitmap = getAssets().open("icon.png");
bmpImageGallery = BitmapFactory.decodeStream(bitmap);
} catch (IOException e) {
e.printStackTrace();
} finally {
bitmap.close();
}
This is how I display an Image from the Gallery via an Intent in the onActivityResult method:
targetURI = data.getData();
try {
bmpImageGallery = MediaStore.Images.Media.getBitmap(this.getContentResolver(), targetURI);
// SET THE IMAGE FROM THE GALLERY TO THE IMAGEVIEW
imgvwSelectedImage.setImageBitmap(bmpImageGallery);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
This is the code to upload the image:
byte[] data = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmpImageGallery.compress(CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
Bundle postImgGallery = new Bundle();
// ADD THE PHOTO DATA TO THE BUNDLE
postImgGallery.putByteArray("photo", data);
// ADD THE CAPTION FROM THE STRING finalStatusMessage TO THE BUNDLE
if (finalStatusMessage.equals("")) {
/***** DO NOTHING HERE *****/
} else {
postImgGallery.putString("caption", finalStatusMessage);
}
Utility.mAsyncRunner.request(userID + "/photos", postImgGallery, "POST", new PhotoUploadListener(), null);
NOTE: In this bit here "caption", finalStatusMessage, the caption can also be substituted with message. I have never seen any difference in the posts using either of these. But do check before using either, just to be safe. ;-)
This class is used to check the status of the upload:
private class PhotoUploadListener extends BaseRequestListener {
#Override
public void onComplete(String response, Object state) {
// DISPLAY A CONFIRMATION TOAST
}
}
The server is sending a list of urls of images when the application loads.
After parsing the url,The Application is Supposed to fetch that images from the server and display those images as MARQUEE on it's header as a BANNER.
On Clicking on that banner...a link is to be open(Say for example link of any WebSite).
Can Anybody tell me how to fetch this image from the url and Save it temporarily and Display them as Banner.
Regards.
I used this code for loding img form url
ImageView v_thumburl = (ImageView) rowView
.findViewById(R.id.v_thumb_url);
thumburl = temp.getString(temp.getColumnIndex("thumburl"));
Drawable drawable = LoadImageFromWebOperations(thumburl);
v_thumburl.setImageDrawable(drawable);
private Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
System.out.println("Exc=" + e);
return null;
}
}
try this i hope it may help u
Use AsyncTask for downloading images from server (Store them in external storage).
After complete the download display those images in Gallery view as a
Banner. (Put Gallery view in banner).
As Android Gallery doesn't support Marquee, use animation for Gallery
view (Like Marquee).
For Loading Image from The Server you can use LasyList which will fetch the image from server and store it into SD Card.
SlideShow will be better than Marquee, so if you want SlideShow see this
And If you want marquee then what you can so is have and HorizontalListView
Add a public Method in HorizontalListView Class as shown below
public void getScrollWidth() {
return mMaxX;
}
public void getCurrentScrollX() {
return mNextX;
}
and For Marquee have a Thread and a Handler in your class like this.
new Thread(new Runnable() {
#Override
public void run() {
try {
handler.post(new Runnable() {
#Override
public void run() {
if((horizontalListView.getCurrentScrollX() + 50) < horizontalListView.getScrollWidth())
{
horizontalListView.scrollTo(horizontalListView.getCurrentScrollX() + 50);
}
else
{
horizontalListView.scrollTo(0);
}
}
});
Thread.sleep(1000);
} catch (Exception e) {
}
}
}).start();
I use the official Facebook SDK in my Android Application. After the user logs in, I can get the uid and the name of the facebook user like so:
Facebook mFacebook = new Facebook(APP_ID);
// ... user logs in ...
//String jsonUser = mFacebook.request("me/picture"); // throws error
String jsonUser = mFacebook.request("me");
JSONObject obj = Util.parseJson(jsonUser);
String facebookId = obj.optString("id");
String name = obj.optString("name");
I also know that the I can access the profile picture with those links:
https://graph.facebook.com/<facebookId>/picture
https://graph.facebook.com/<facebookId>/picture?type=large
I would love to use this code to geht the profile picture:
public static Drawable getPictureForFacebookId(String facebookId) {
Drawable picture = null;
InputStream inputStream = null;
try {
inputStream = new URL("https://graph.facebook.com/" + facebookId + "/picture").openStream();
} catch (Exception e) {
e.printStackTrace();
return null;
}
picture = Drawable.createFromStream(inputStream, "facebook-pictures");
return picture;
}
But it just wont work. I always get the following error:
SSL handshake failure: Failure in SSL library, usually a protocol error
And I cant solve this issue. It seems to be rather complicated(look here or here). So what other options are there to get the picture of a facebook user that successfully logged into my application?
ImageView user_picture;
userpicture=(ImageView)findViewById(R.id.userpicture);
URL img_value = null;
img_value = new URL("http://graph.facebook.com/"+id+"/picture?type=large");
Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
userpicture.setImageBitmap(mIcon1);
where ID is ur profile ID...
I also had that problem some time ago. What I did was download the picture using an async task, and then set an ImageView with the image just downloaded. I will paste the code snippet:
ImageView fbUserAvatar = (ImageView) findViewById(R.id.fb_user_avatar);
private synchronized void downloadAvatar() {
AsyncTask<Void, Void, Bitmap> task = new AsyncTask<Void, Void, Bitmap>() {
#Override
public Bitmap doInBackground(Void... params) {
URL fbAvatarUrl = null;
Bitmap fbAvatarBitmap = null;
try {
fbAvatarUrl = new URL("http://graph.facebook.com/"+USER_ID+"/picture");
fbAvatarBitmap = BitmapFactory.decodeStream(fbAvatarUrl.openConnection().getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return fbAvatarBitmap;
}
#Override
protected void onPostExecute(Bitmap result) {
fbUserAvatar.setImageBitmap(result);
}
};
task.execute();
}
This code works for me. I hope it works for you too.
You can request a direct URl which contains your Access token:
URL MyProfilePicURL = new URL("https://graph.facebook.com/me/picture?type=normal&method=GET&access_token="+ Access_token );
Then get a decoded BitMap and assign it to image view:
Bitmap MyprofPicBitMap = null;
try {
MyprofPicBitMap = BitmapFactory.decodeStream(MyProfilePicURL.openConnection().getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MyProfilePicImageView.setImageBitmap(mIcon1);
request("me/picture") throws an error because the server returns a 302 (redirect to the image url) and the facebook sdk does not handle this.
For displaying profile pic in your app, use ProfilePictureView from Facebook SDK.
Refer This
Just call setProfileId(String profileId) on it.
It will take care of displaying the image.
Add one line of code and that will be resolved.
HttpURLConnection.setFollowRedirects(true);
Use this, (usuario is a GraphUser):
ProfilePictureView p;
p = (ProfilePictureView) rootView.findViewById(R.id.fotoPerfil);
p.setProfileId(usuario.getId());
and xml markup:
<com.facebook.widget.ProfilePictureView
android:id="#+id/profilePicture"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center_horizontal"
android:layout_marginBottom="10dp"
facebook:preset_size="normal"/>