Download gif from given URL - android

I'm trying to download GIF from given Url but there is a problem. I'm stuck here and don't know the correct way to download this file. How to repair this? I have sample url: http://i1.kwejk.pl/k/obrazki/2015/02/6f0239004a643dbd9510ebda5a6f22b3.gif
My code:
try{
System.out.println(this.adressToGif);
URL url = new URL(urltoGif);
URLConnection conn = url.openConnection();
//conn.connect();
InputStream is = conn.getInputStream(); //IT crashes here and jumps to catch()
BufferedInputStream bis = new BufferedInputStream(is);
bis.mark(conn.getContentLength());
bis.close();
gifPlayer = new NewGifPlayer(this, bis);
//Movie movie = Movie.decodeStream(bis);
}catch(Exception x)
{
System.out.println("There is a problem");
}

You cannot run networking operations on the main thread. Use AsyncTask for that.

Related

Making Sprite from image from web/server in AndEngine

I want to make a Sprite from an image which is placed on my server I have complete path of the image. Is it possible to do it in AndEngine.? Currently I am working in GLES 2.
first you need generate a link to your image and after,watch this code for load the image in a TextureRegion using HttpURLConnection and InputStream:
try {
ITexture mTexture = new BitmapTexture(pEngine.getTextureManager(), new IInputStreamOpener() {
#Override
public InputStream open() throws IOException {
URL url = new URL("https://yourImage.png");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
BufferedInputStream in = new BufferedInputStream(input);
return in;
}
});
mTexture.load();
TextureRegion MyImageFromWeb = TextureRegionFactory.extractFromTexture(mTexture);
} catch (IOException e) {
Debug.e(e);
}
it is possible you just have to download it first with HttpGet or UrlConnection in Background Thread
But i would recommend you to download all your assets in some kind of splash screen and saving them into sdcard

HttpURLConnection returns 404 when called from an IntentService

I am attempting to build an IntentService that uploads files using a REST Api.
I am able to start the IntentService from my main Activity, and the IntentService can pass a message back to the Activity so I know the IntentService is working.
My issue is when I run this code:
#Override
protected void onHandleIntent(Intent intent) {
// do the uploading stuff
try {
URL url = new URL(this.url + fileName);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("PUT");
conn.setDoOutput(true);
BufferedInputStream buffInputStream = new BufferedInputStream(new FileInputStream(filePath));
BufferedOutputStream buffOutputStream = new BufferedOutputStream(conn.getOutputStream());
HttpUtils.copyBufferStream(buffInputStream, buffOutputStream);
if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
BufferedInputStream responseBufferedInput = new BufferedInputStream(conn.getInputStream());
byte[] body = HttpUtils.readStream(responseBufferedInput);
result = HttpUtils.convertBytesToString(body);
Log.e("Result:", result);
}else{
Log.e("conn.getResponseCode()", Integer.toString(conn.getResponseCode()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
in the IntentService onHandleIntent method, I always get a 404 (or FileNotFoundException when not checking conn.getResponseCode()).
Calling the exact same code in my main Activity, uploads the file and completes successfully.
I have no idea why this is happening? Any ideas?
Try printing the URL you're trying to open, you will probably find out that there's something wrong with it...maybe it's just a missing slash ;-)
404 is the HTTP error for not found..meaning that the URL is invalid on that server.

android :images not being displayed

I am making an android app in which i need to display the remote images in my app
my using following code.
but the images are not being displayed:
for(int i=0;i<stringOnTextView.length;i++){
imageUrl = "http://ondamove.it/English/images/users/";
imageUrl = imageUrl+stringOnTextView[i];
System.out.println(imageUrl);
URL myFileUrl = new URL(imageUrl);
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
image.setImageBitmap(bmImg);
System.out.println(bmImg.toString());
}
Can anyone tell me where is the problem. thanks
as a completion of my comment , see this three tutorials and you will find out what you should do :) :
tuto1
tuto2
tuto3

Pulling images from Web

I'm using the following code to grab images from the web. It uses Gridview and depending on position, picks a URL from an array. It works but the loading of images is often hit or miss. Almost every time I start the app, a different number of images load.
It also has issues when changing from portrait to landscape view, 5 out of 10 images may be displayed then I'll turn the device and usually lose all the images. Sometimes a few do show up though.
Any ideas on making this more robust?
try {
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
return bm;
} catch (IOException e) {
Log.d("DEBUGTAG", "error...");
}
return null;
One thing I read is that there's a known bug with decoding Bitmaps from an InputStream, and the suggested fix from Google was to use a FlushedInputStream (example in below URL):
http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html
Also, I'd put the download code into an AsyncTask. Here's what I currently use for mine:
public static Bitmap loadImageFromUri(URI uri)
{
URL url;
try {
url = uri.toURL();
} catch (MalformedURLException e) {
Log.v("URL Exception", "MalformedURLException");
return null;
}
try
{
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(new FlushedInputStream(input));
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
I just pass in a URI due to the way I've got the rest of my code set up, you could pass in a URL instead and skip the first part. This will keep the download from tying up your UI thread.

How to work with an image using url in android?

Given a Url for an image, I want to downoload it and paste it onto my canvas in android. How do I retrieve the image into my app ?
Please help.
Thanks,
de costo.
Dont forget to give the app the permission to connect to the Web,
in the AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
You can use the following code to download an image:
URLConnection connection = uri.toURL().openConnection();
connection.connect();
InputStream is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is, 8 * 1024);
Bitmap bmp = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
Requires the following permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
There is a an HTTP client library that might be supported in Android now, but for any fine grain control you can use URL & HttpURLConnection. the code will look something like this:
URL connectURL = new URL(<your URL goes here>);
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
// do some setup
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("GET");
// connect and flush the request out
conn.connect();
conn.getOutputStream().flush();
// now fetch the results
String response = getResponse(conn);
where getResponse() looks something like this, in your case you are getting
a pile of binary data back you might want to change the StringBuffer to a byte array
and chunk the reads by a larger increment.
private String getResponseOrig(HttpURLConnection conn)
{
InputStream is = null;
try
{
is = conn.getInputStream();
// scoop up the reply from the server
int ch;
StringBuffer sb = new StringBuffer();
while( ( ch = is.read() ) != -1 ) {
sb.append( (char)ch );
}
return sb.toString();
}
catch(Exception e)
{
Log.e(TAG, "biffed it getting HTTPResponse");
}
finally
{
try {
if (is != null)
is.close();
} catch (Exception e) {}
}
return "";
}
As you are talking about image data which can be large, other things you need to be assiduous about in Android is making sure you release your memory as soon as you can, you've only got 16mb of heap to play with for all apps and it runs out fast and the GC will drive you nuts if you aren't really good about giving back memory resources

Categories

Resources