您好我学习了如何从SQL中获取数据并显示到TextView上。但是我在显示图像方面遇到了一些麻烦。
图像作为URL存储在SQL数据库中,但我无法在视图中显示为图像。
我做了一些研究并发现了“如何使用给定的URL显示图像”,但我只是在理解抓取URL和显示的概念时遇到一点困难。请帮助我。谢谢!
这是我得到的错误日志。
更新 第159行:photoMe.setImageDrawable(drawable);
11-15 14:53:21.450: W/LoadImageFromWebOperations(28444): java.net.MalformedURLException: Protocol not found: photo
11-15 14:53:21.480: D/AndroidRuntime(28444): Shutting down VM
11-15 14:53:21.480: W/dalvikvm(28444): threadid=1: thread exiting with uncaught exception (group=0x40c55a68)
11-15 14:53:21.500: E/AndroidRuntime(28444): FATAL EXCEPTION: main
11-15 14:53:21.500: E/AndroidRuntime(28444): java.lang.NullPointerException
11-15 14:53:21.500: E/AndroidRuntime(28444): at com.app.android.DirectoryDetailMeActivity$GetDirectoryDetails$1.run(DirectoryDetailMeActivity.java:159)
11-15 14:53:21.500: E/AndroidRuntime(28444): at android.os.Handler.handleCallback(Handler.java:605)
11-15 14:53:21.500: E/AndroidRuntime(28444): at android.os.Handler.dispatchMessage(Handler.java:92)
11-15 14:53:21.500: E/AndroidRuntime(28444): at android.os.Looper.loop(Looper.java:137)
11-15 14:53:21.500: E/AndroidRuntime(28444): at android.app.ActivityThread.main(ActivityThread.java:4517)
11-15 14:53:21.500: E/AndroidRuntime(28444): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 14:53:21.500: E/AndroidRuntime(28444): at java.lang.reflect.Method.invoke(Method.java:511)
11-15 14:53:21.500: E/AndroidRuntime(28444): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
11-15 14:53:21.500: E/AndroidRuntime(28444): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
11-15 14:53:21.500: E/AndroidRuntime(28444): at dalvik.system.NativeStart.main(Native Method)
public class DirectoryDetailMeActivity extends Activity {
ImageView photoMe;
TextView txtName;
String uid;
String photo = "";
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static final String url_veiw_directory = "directory_detail_me.php";
private static final String TAG_ID = "uid";
private static final String TAG_IMG = "photo";
private static final String TAG_NAME = "name";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.directory_detail_me);
uid = i.getStringExtra(TAG_ID);
new GetDirectoryDetails().execute();
}
class GetDirectoryDetails extends AsyncTask<String, String, String> {
protected String doInBackground(String... params) {
runOnUiThread(new Runnable() {
public void run() {
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", uid));
JSONObject json = jsonParser.makeHttpRequest(url_veiw_directory, "GET", params);
Log.d("my profile", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
JSONArray directoryObj = json.getJSONArray(TAG_DIRECTORY);
JSONObject directory = directoryObj.getJSONObject(0);
txtName.setText(directory.getString(TAG_NAME));
Drawable drawable = LoadImageURL(TAG_IMG);
photoMe.setImageDrawable(drawable);
txtName = (TextView) findViewById(R.id.name);
photoMe = (ImageView) findViewById(R.id.photo);
}else{
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private Drawable LoadImageURL(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "photo");
return d;
}
catch (Exception e)
{
Log.w("LoadImageURL",e.toString());
return null;
}
}
});
return null;
}
}
}
答案 0 :(得分:0)
您似乎不了解AsyncTask。关键是它不能在UI线程上运行,因此可以像HTTP连接一样进行长期IO。在runOnUIThread中包装整个doInBackground与此完全相反。它不是你唯一的问题,但它在列表中。
由于格式化,其余部分难以阅读,但请查看第159行。您在那里使用的某些变量为空。