我正在尝试从URL
到ImageView
获取图像我正在使用this类。我已经添加了连接到internet write_SD卡的权限等等,AndroidManifest
问题是当我尝试使用此函数将图像添加到java.lang.NullPointerException
时它返回错误ImageView
:`< / p>
public void download(String url, ImageView imageView) {
try
{
if (cancelPotentialDownload(url, imageView))
{
//TODO:Caching code right here
String filename = String.valueOf(url.hashCode());
File f = new File(getCacheDirectory(hm), filename);
// Is the bitmap in our cache?
Bitmap bitmap = BitmapFactory.decodeFile(f.getPath());
//No? download it
if(bitmap == null){
BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task);
imageView.setImageDrawable(downloadedDrawable);
task.execute(url);
}else{
//Yes? set the image
imageView.setImageBitmap(bitmap);
}
}
}catch (Exception e) {
// TODO: handle exception
Log.e("",e.toString());
}
}`
问题是什么可能是错的,我怎么能解决这个问题? 如果你能指出我正确的方向,我将很高兴。
logcat的:
`06-06 09:38:19.686: E/AndroidRuntime(18832): java.lang.NullPointerException
06-06 09:38:19.686: E/AndroidRuntime(18832): at com.jandans.geos.ImageDownloader.download(ImageDownloader.java:51)
06-06 09:38:19.686: E/AndroidRuntime(18832): at com.jandans.geos.vaikpied$1.onItemClick(vaikpied.java:98)
06-06 09:38:19.686: E/AndroidRuntime(18832): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
06-06 09:38:19.686: E/AndroidRuntime(18832): at android.widget.ListView.performItemClick(ListView.java:3513)
06-06 09:38:19.686: E/AndroidRuntime(18832): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1849)
06-06 09:38:19.686: E/AndroidRuntime(18832): at android.os.Handler.handleCallback(Handler.java:587)
06-06 09:38:19.686: E/AndroidRuntime(18832): at android.os.Handler.dispatchMessage(Handler.java:92)
06-06 09:38:19.686: E/AndroidRuntime(18832): at android.os.Looper.loop(Looper.java:130)
06-06 09:38:19.686: E/AndroidRuntime(18832): at android.app.ActivityThread.main(ActivityThread.java:3835)
06-06 09:38:19.686: E/AndroidRuntime(18832): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 09:38:19.686: E/AndroidRuntime(18832): at java.lang.reflect.Method.invoke(Method.java:507)
06-06 09:38:19.686: E/AndroidRuntime(18832): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
06-06 09:38:19.686: E/AndroidRuntime(18832): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
06-06 09:38:19.686: E/AndroidRuntime(18832): at dalvik.system.NativeStart.main(Native Method)
`
答案 0 :(得分:1)
以下代码段会对您有所帮助。
<强> DownloadHelper.java 强>
public interface DownloadHelper
{
public void OnSucess(Bitmap bitmap);
public void OnFailure(String response);
}
<强> MainActivity.java 强>
public class GalleryExample extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
DownloadHelper downloadHelper = new DownloadHelper()
{
@Override
public void OnSucess(Bitmap bitmap)
{
ImageView imageView=(ImageView)findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
}
@Override
public void OnFailure(String response)
{
Toast.makeText(context, response, Toast.LENGTH_LONG).show();
}
};
new MyTask(this,downloadHelper).execute("image url");
}
<强> MyTask.java 强>
public class DownloadTask extends AsyncTask<String, Integer, Object>
{
private Context context;
private DownloadHelper downloadHelper;
private ProgressDialog dialog;
public DownloadTask(Context context,DownloadHelper downloadHelper)
{
this.context = context;
}
@Override
protected void onPreExecute()
{
dialog = new ProgressDialog(context);
dialog.setTitle("Please Wait");
dialog.setMessage("Fetching Data!!");
dialog.setCancelable(false);
dialog.show();
super.onPreExecute();
}
@Override
protected Object doInBackground(String... params)
{
URL aURL = new URL(myRemoteImages[position]);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/* Decode url-data to a bitmap. */
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
return bm;
}
@Override
protected void onPostExecute(Object result)
{
dialog.dismiss();
if (result != null)
{
downloadHelper.OnSucess((Bitmap)result);
}
else
{
downloadHelper.OnFailure("Error in Downloading Data!!");
}
super.onPostExecute(result);
}
}
答案 1 :(得分:1)
尝试下面的一个 -
public class ImgLoadFrmWebActivity extends Activity {
ImageView imv1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Drawable image = ImageOperations(getApplicationContext(),"http://hvz.eu5.org/upload/1.jpg","image");
ImageView imgView = (ImageView)findViewById(R.id.imageView1);
imgView = (ImageView)findViewById(R.id.imageView1);
imgView.setImageDrawable(image);
}
private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public Object fetch(String address) throws MalformedURLException,IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
}
确保在清单文件中添加了权限 -
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
只需根据您的需求自定义此布局。希望这会对你有所帮助。