Czy możliwe jest użycie BitmapFactory.decodeFile metoda dekodowania obrazu z lokalizacji http?
Chciałbym wiedzieć, czy możliwe jest użycie BitmapFactory.decodeFile metoda dekodowania obrazu z lokalizacji http?
Dla np.
ImageView imageview = new ImageView(context);
Bitmap bmp = BitmapFactory.decodeFile("http://<my IP>/test/abc.jpg");
imageview.setImageBitmap(bmp);
Ale bmp zawsze zwraca null.
Czy Jest jakiś inny sposób, aby osiągnąć ten scenariusz, w którym mam zestaw obrazów w moim komputerze na serwerze i ładuję obrazy do mojej aplikacji galerii za pomocą xml?
Dzięki,
Sen
5 answers
Użyj decodeStream i zamiast tego podaj adres URL.
Oto przykład:
Bitmap bmp = BitmapFactory.decodeStream(new java.net.URL(url).openStream())
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-12-22 14:42:50
@Amir & @Sankar : dzięki za cenne sugestie.
Rozwiązałem powyższy problem wykonując poniższy fragment kodu:
ImageView iv = new ImageView(context);
try{
String url1 = "http://<my IP>/test/abc.jpg";
URL ulrn = new URL(url1);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
iv.setImageBitmap(bmp);
else
System.out.println("The Bitmap is NULL");
} catch(Exception e) {
}
Dzięki,
Sen
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-22 04:02:38
String urldisplay="http://www.google.com/";//sample url
Log.d("url_dispaly",urldisplay);
try{
InputStream in = new java.net.URL(urldisplay).openStream();
Bitmap mIcon11 = BitmapFactory.decodeStream(new SanInputStream(in));
}
catch(Exception e){}
Utwórz nazwę klasy SanInputStream
public class SanInputStream extends FilterInputStream {
public SanInputStream(InputStream in) {
super(in);
}
public long skip(long n) throws IOException {
long m = 0L;
while (m < n) {
long _m = in.skip(n-m);
if (_m == 0L) break;
m += _m;
}
return m;
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-12-22 14:56:52
Jeśli się nie mylę, @ sen code snippet powinien zwrócić null W przypadku .Plik BMP i logcat powinny się zalogować:
skia decoder->decode returned false
Jeśli coś takiego się dzieje, spróbuj użyć tego kodu (działa również w przypadku wejścia bitmapy):
HttpGet httpRequest = null;
try {
httpRequest = new HttpGet(url.toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();
bmp = BitmapFactory.decodeStream(instream);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-08 08:35:06
// Utwórz obiekt dla podklasy AsyncTask
GetXMLTask task = new GetXMLTask();
/ / Wykonaj zadanie
task.execute(new String[] { "ImageURL" });
/ / Następnie w klasie Asyntask Przypisz obraz do widoku obrazu, aby uniknąć Androida.os.NetworkOnMainThreadException
private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... urls) {
Bitmap map = null;
for (String url : urls) {
map = downloadImage(url);
}
return map;
}
// Sets the Bitmap returned by doInBackground
@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
// Creates Bitmap from InputStream and returns it
private Bitmap downloadImage(String url) {
Bitmap bitmap = null;
InputStream stream = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
try {
stream = getHttpConnection(url);
bitmap = BitmapFactory.
decodeStream(stream, null, bmOptions);
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString)
throws IOException {
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
try {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
stream = httpConnection.getInputStream();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return stream;
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-12 15:04:03