0

嗨,我学会了如何从 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;
        }

    }
}
4

1 回答 1

0

您似乎不了解 AsyncTask。关键是它不在 UI 线程上运行,因此可以像 HTTP 连接那样进行长期 IO。将整个 doInBackground 包装在 runOnUIThread 中与此完全相反。它不是你唯一的问题,但它在列表中。

由于格式化,其余部分难以阅读,但请查看第 159 行。您正在使用的某些变量为空。

于 2012-11-15T20:25:40.650 回答