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
Related
I've read that wrapping a BufferedInputStream around an input stream is only of benefit if you're reading the input stream in small chunks. Otherwise, using it may actually have adverse effects.
What is the situation when the input stream is bitmap data fetched with HttpURLConnection (or your favourite networking library, e.g. OkHttp)... is it a help or a hindrance?
I'm wondering not only in terms of overall time/speed, but also in terms of resiliency... would using a buffer help at all with flaky network conditions where the connection is dropping in and out?
boolean useBufferedInputStream = true; // <--- true or false?
URL url = new URL("https://example.com/my_bitmap.png");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream;
if (useBufferedInputStream) {
inputStream = new BufferedInputStream(connection.getInputStream());
} else {
inputStream = connection.getInputStream();
}
Bitmap bmp = BitmapFactory.decodeStream(inputStream);
I'm having trouble with loading an image from a URL into my Android app using the Google Places API.
I got the part with the API working and I can see the image if I type the URL in my browser. The code fails in the below line:
Bitmap myBitmap = BitmapFactory.decodeStream(input);
The strange thing is, it simply jumps to the return null in the catch clause without executing the println commands.
I should also explain that I'm using two URL's since the first URL in the google API is a redirect page.
I assume that the problem is that the final page that's being loaded isn't actually a "real" image (I mean it looks like
https://lh4.googleusercontent.com/XXX/s1600-w400/
rather than something like
http://something/image.jpg)
Below is the entire method I'm using:
public Bitmap getImageData(Place p) {
try {
String src= "https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference="+
p.getPhoto_reference()+
"&key="+
this.context.getResources().getString(places_api);
Log.d("srcsrc", src);
URL url = new URL(src);
HttpsURLConnection ucon = (HttpsURLConnection) url.openConnection();
ucon.setInstanceFollowRedirects(false);
URL secondURL = new URL(ucon.getHeaderField("Location"));
HttpsURLConnection connection = (HttpsURLConnection) secondURL.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Bitmap exception" + e);
return null;
}
}
Appreciate your help.
use Picasso like this :
Picasso.with(context).load(src).into(imageView );
It will do the following:
Handling ImageView recycling and download cancelation in an adapter.
Complex image transformations with minimal memory use.
Automatic memory and disk caching.
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.
I am getting MalformedURLException: Protocol not found, when I am trying to load image from URL sting. That image is displaying in Browser when I paste that URL. But not displaying in ImageView. I've used this code:
public static Bitmap getBitmapFromURL(String src) {
try
{
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
}
catch (IOException e) {
e.printStackTrace();
return null;
}
}
Please anybody tell me whats the problem? It will be very helpful to me.
Its a
http://74.208.68.90/webservice/images/image44.png
Your code is OK.
malformedexception comes when there is some problem in your url
try to encode your parameters in url with url encoder and protocall not found shows
http is missing from your url
String url = url +"/" + UrlEncoder.encode(param);
I think it is "src" that may have problem,
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.