我试图设计一个应用程序,而我想让它做的就是从URL加载图像。我用URL(来自互联网)显示GIF图像但是它没有显示图像。我有发布的代码以及下面的logcat:
Activity
package com.example.mosdacappalert_radio;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RelativeLayout;
public class Heavy_rain1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_heavy_rain1);
RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.addView);
myLayout.addView(new com.example.mosdacappalert_radio.ShowGifView(this,
"http://graphico.in/wp-content/uploads/2014/05/Happy-anniversary-wishes-to-you.gif"));
}
}
class file
package com.example.mosdacappalert_radio;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.view.View;
public class ShowGifView extends View {
// Set true to use decodeStream
// Set false to use decodeByteArray
private static final boolean DECODE_STREAM = true;
private InputStream gifInputStream;
private Movie gifMovie;
private int movieWidth, movieHeight;
private long movieDuration;
private long mMovieStart;
static String gifURL;
public ShowGifView(Context context, String a) {
super(context);
gifURL = a;
init(context);
}
public ShowGifView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public ShowGifView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(final Context context) {
setFocusable(true);
gifMovie = null;
movieWidth = 0;
movieHeight = 0;
movieDuration = 0;
Loder task = new Loder();
task.execute(new String[] { gifURL });
}
private static byte[] streamToBytes(InputStream is) {
ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
int len;
try {
while ((len = is.read(buffer)) >= 0) {
os.write(buffer, 0, len);
}
} catch (java.io.IOException e) {
}
return os.toByteArray();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(movieWidth, movieHeight);
}
public int getMovieWidth() {
return movieWidth;
}
public int getMovieHeight() {
return movieHeight;
}
public long getMovieDuration() {
return movieDuration;
}
@Override
protected void onDraw(Canvas canvas) {
long now = android.os.SystemClock.uptimeMillis();
if (mMovieStart == 0) { // first time
mMovieStart = now;
}
if (gifMovie != null) {
int dur = gifMovie.duration();
if (dur == 0) {
dur = 1000;
}
int relTime = (int) ((now - mMovieStart) % dur);
gifMovie.setTime(relTime);
gifMovie.draw(canvas, 0, 0);
invalidate();
}
}
private class Loder extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
URL gifURL;
try {
gifURL = new URL(urls[0]);
HttpURLConnection connection = (HttpURLConnection) gifURL
.openConnection();
gifInputStream = connection.getInputStream();
// Insert dummy sleep
// to simulate network delay
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (DECODE_STREAM) {
gifMovie = Movie.decodeStream(gifInputStream);
} else {
byte[] array = streamToBytes(gifInputStream);
gifMovie = Movie.decodeByteArray(array, 0, array.length);
}
movieWidth = gifMovie.width();
movieHeight = gifMovie.height();
movieDuration = gifMovie.duration();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
invalidate();
requestLayout();
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/addView"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</RelativeLayout>
</LinearLayout>
Logcat:
12-08 19:18:37.408: W/System.err(4521): java.io.IOException
12-08 19:18:37.428: W/System.err(4521): at java.util.zip.InflaterInputStream.reset(InflaterInputStream.java:279)
12-08 19:18:37.428: W/System.err(4521): at android.graphics.Movie.decodeStream(Native Method)
12-08 19:18:37.428: W/System.err(4521): at com.example.mosdacappalert_radio.ShowGifView$Loder.doInBackground(ShowGifView.java:136)
12-08 19:18:37.428: W/System.err(4521): at com.example.mosdacappalert_radio.ShowGifView$Loder.doInBackground(ShowGifView.java:1)
12-08 19:18:37.428: W/System.err(4521): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-08 19:18:37.428: W/System.err(4521): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-08 19:18:37.428: W/System.err(4521): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-08 19:18:37.438: W/System.err(4521): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-08 19:18:37.438: W/System.err(4521): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-08 19:18:37.438: W/System.err(4521): at java.lang.Thread.run(Thread.java:841)
12-08 19:18:37.438: D/skia(4521): ------- reset threw an exception
12-08 19:18:37.438: W/System.err(4521): java.io.IOException
12-08 19:18:37.448: W/System.err(4521): at java.util.zip.InflaterInputStream.reset(InflaterInputStream.java:279)
12-08 19:18:37.448: W/System.err(4521): at android.graphics.Movie.decodeStream(Native Method)
12-08 19:18:37.448: W/System.err(4521): at com.example.mosdacappalert_radio.ShowGifView$Loder.doInBackground(ShowGifView.java:136)
12-08 19:18:37.448: W/System.err(4521): at com.example.mosdacappalert_radio.ShowGifView$Loder.doInBackground(ShowGifView.java:1)
12-08 19:18:37.448: W/System.err(4521): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-08 19:18:37.448: W/System.err(4521): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-08 19:18:37.448: W/System.err(4521): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-08 19:18:37.448: W/System.err(4521): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-08 19:18:37.448: W/System.err(4521): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-08 19:18:37.448: W/System.err(4521): at java.lang.Thread.run(Thread.java:841)
12-08 19:18:37.448: D/skia(4521): ------- reset threw an exception
12-08 19:18:37.498: W/System.err(4521): java.io.IOException
12-08 19:18:37.518: W/System.err(4521): at java.util.zip.InflaterInputStream.reset(InflaterInputStream.java:279)
12-08 19:18:37.518: W/System.err(4521): at android.graphics.Movie.decodeStream(Native Method)
12-08 19:18:37.518: W/System.err(4521): at com.example.mosdacappalert_radio.ShowGifView$Loder.doInBackground(ShowGifView.java:136)
12-08 19:18:37.518: W/System.err(4521): at com.example.mosdacappalert_radio.ShowGifView$Loder.doInBackground(ShowGifView.java:1)
12-08 19:18:37.518: W/System.err(4521): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-08 19:18:37.518: W/System.err(4521): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-08 19:18:37.518: W/System.err(4521): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-08 19:18:37.518: W/System.err(4521): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-08 19:18:37.518: W/System.err(4521): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-08 19:18:37.518: W/System.err(4521): at java.lang.Thread.run(Thread.java:841)
12-08 19:18:37.518: D/skia(4521): ------- reset threw an exception
12-08 19:18:37.518: W/System.err(4521): java.io.IOException
12-08 19:18:37.518: W/System.err(4521): at java.util.zip.InflaterInputStream.reset(InflaterInputStream.java:279)
12-08 19:18:37.518: W/System.err(4521): at android.graphics.Movie.decodeStream(Native Method)
12-08 19:18:37.528: W/System.err(4521): at com.example.mosdacappalert_radio.ShowGifView$Loder.doInBackground(ShowGifView.java:136)
12-08 19:18:37.528: W/System.err(4521): at com.example.mosdacappalert_radio.ShowGifView$Loder.doInBackground(ShowGifView.java:1)
12-08 19:18:37.528: W/System.err(4521): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-08 19:18:37.528: W/System.err(4521): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-08 19:18:37.528: W/System.err(4521): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-08 19:18:37.528: W/System.err(4521): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-08 19:18:37.528: W/System.err(4521): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-08 19:18:37.528: W/System.err(4521): at java.lang.Thread.run(Thread.java:841)
12-08 19:18:37.528: D/skia(4521): ------- reset threw an exception
12-08 19:18:37.528: W/System.err(4521): java.io.IOException
12-08 19:18:37.528: W/System.err(4521): at java.util.zip.InflaterInputStream.reset(InflaterInputStream.java:279)
12-08 19:18:37.528: W/System.err(4521): at android.graphics.Movie.decodeStream(Native Method)
12-08 19:18:37.528: W/System.err(4521): at com.example.mosdacappalert_radio.ShowGifView$Loder.doInBackground(ShowGifView.java:136)
12-08 19:18:37.528: W/System.err(4521): at com.example.mosdacappalert_radio.ShowGifView$Loder.doInBackground(ShowGifView.java:1)
12-08 19:18:37.528: W/System.err(4521): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-08 19:18:37.528: W/System.err(4521): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-08 19:18:37.528: W/System.err(4521): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-08 19:18:37.528: W/System.err(4521): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-08 19:18:37.528: W/System.err(4521): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-08 19:18:37.528: W/System.err(4521): at java.lang.Thread.run(Thread.java:841)
12-08 19:18:37.538: D/skia(4521): ------- reset threw an exception
12-08 19:18:37.538: D/skia(4521): QURAMWINKI_ParseGIFHeader_SKIA error
12-08 19:18:37.538: D/skia(4521): Wink AGIF Move Constructer End 0, totalTime : 0
12-08 19:18:37.558: D/skia(4521): Wink AGIF onGetBitmap Null
答案 0 :(得分:0)
从this SE answer来看,使用Movie
类的动画GIF支持看起来有些问题(它不适用于较旧的API,并且在某些类型的GIF编码上失败)。使用WebView
可能更简单。
答案 1 :(得分:0)
您可以使用
电影课: - 在Android的API级别1中引入,但GIF解码不适用于所有设备。某些设备的某些版本遇到一致性问题,例如在Xperia L和2.3.3上的设备无效等。
为了避免上述问题,只需使用 WebView ,只需加载GIF网址即可 它