我对编程很陌生,我唯一的体验是非常轻松的C#和javascript。
我想创建一个简单的Android应用程序,只显示一张图片和一些来自网络的数据但是我只是在显示图像时遇到了麻烦(还没有尝试过其他任何东西)。我搜索了SO和网络上的代码示例,但无法让它们工作。
这是我的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".TestActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
和我的main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
和我唯一的活动
package my.test;
import java.io.InputStream;
import java.net.URL;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imgView =(ImageView)findViewById(R.id.imageView1);
Drawable drawable = LoadImageFromWebOperations("http://www.androidpeople.com/wp-content/uploads/2010/03/android.png");
imgView.setImageDrawable(drawable);
}
private Drawable LoadImageFromWebOperations(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
}
我尝试过使用AVD和手机的其他例子,结果相同:空白屏幕。我究竟做错了什么?
答案 0 :(得分:1)
public Bitmap FetchImage(URL url) {
try {
URLConnection conn = url.openConnection();
conn.connect();
final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
final Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
return bm;
} catch (IOException e) {}
return null;
}
现在您只需使用ImageView中的setImageBitmap方法来显示图像。 确保你在AsyncTask中下载部分。
以下代码段将为您提供帮助
<强> 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 :(得分:0)
可以尝试
imgView.setImageURI(Uri.parse("http://www.androidpeople.com/wp-content/uploads/2010/03/android.png"));
或者如果你需要drwaable
答案 2 :(得分:0)
授予权限
<uses-permission android:name = "android.permission.INTERNET"/>
在您的清单中并检查您的手机是否已连接到网络。你的代码适合我。
答案 3 :(得分:0)
在尝试了所有我可以谷歌并想到之后,我决定尝试使用Android 2.1版代码而不是最新版本,你知道这一切都神奇地起作用。我已经在我的问题中尝试了成功的例子以及答案中的一两个例子也取得了成功。感谢所有帮助和SO。
虽然如果有人能够分享为什么看似有效的代码在我为2.1而不是4.0.3构建时有效的想法,我会很高兴。 BTW在我的手机上进行了测试,运行4.0.3
答案 4 :(得分:-2)
使用WebView代替ImageView,如下所示:
html = "<html><img src=\"" + imageUrl
+ "\"></html>";
final WebView webView = (WebView) view
.findViewById(R.id.yourWebView);
wvImageView.getSettings().setJavaScriptEnabled(true);
wvImageView.loadDataWithBaseURL("", html, "text/html", "utf-8", "");
This should display your image on WebView. If you want to provide zooming facility, try this:
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
这可以解决你的问题:)