图像显示在模拟器中,但不在Android手机中

时间:2015-03-13 10:00:30

标签: android

我在这里尝试从URL获取图像。图像在模拟器中加载良好,但在Android设备中不加载。 toast消息显示在设备中。我尝试了所提到的解决方案,但它们并没有很好地运作。

protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.employee_info_activity);

        back_btn = (Button) findViewById(R.id.back_button);
        empId_edt = (EditText) findViewById(R.id.emp_id);
        name_edt = (EditText) findViewById(R.id.emp_name);
        mobileNo_edt = (EditText) findViewById(R.id.emp_mobile_no);
        panNo_edt = (EditText) findViewById(R.id.emp_pan_no);
        passportNo_edt = (EditText) findViewById(R.id.emp_passport_no);
        emailId_edt = (EditText) findViewById(R.id.emp_email);
        address_edt = (EditText) findViewById(R.id.emp_addr);
        profilePhoto_imgvw = (ImageView) findViewById(R.id.profile_pic);

        empid = SharedPreferncesUtility.getempId(context);
        password = SharedPreferncesUtility.getPassword(context);

        url = "http://xxx.xxx.x.xx:xxxx/XXXX/downloadAction.do?empId="+empid+"&folderName=PHOTO";

        back_btn.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                Intent intent = new Intent(EmployeeInfoActivity.this, HomeScreenActivity.class);
                startActivity(intent);
            }
        });

        new EmployeeProfilePhotoAsyncTask(context, url).execute();
    }

    final class EmployeeProfilePhotoAsyncTask extends AsyncTask<String, Void, String>
    {
        private Context context;
        private ProgressDialog dlg;
        private String url;

        public EmployeeProfilePhotoAsyncTask(Context context, String url)
        {
            this.context = context;
            this.url = url;
        }

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();

            dlg = new ProgressDialog(EmployeeInfoActivity.this);
            dlg.setCanceledOnTouchOutside(false);
            dlg.setCancelable(false);
            dlg.setMessage("Please wait...");
            dlg.show();
        }

        @Override
        protected String doInBackground(String... params)
        {
            try
            {
                URL link = new URL(url);
                profilePhoto = BitmapFactory.decodeStream(link.openConnection().getInputStream());

            }
            catch (MalformedURLException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return url;
        }
        protected void onPostExecute(String result)
        {
            dlg.dismiss();
            String jsonString = gJSON.generateJSON(empid, password);
            new EmployeeProfileAsync(context, jsonString).execute();
        }
    }

    final class EmployeeProfileAsync extends AsyncTask<String, Void, String>
    {
        private Context context;
        private ProgressDialog dlg;

        private String jsonData;

        public EmployeeProfileAsync(Context context, String jsonData)
        {
            this.context = context;
            this.jsonData = jsonData;
        }

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();

            dlg = new ProgressDialog(EmployeeInfoActivity.this);
            dlg.setCanceledOnTouchOutside(false);
            dlg.setCancelable(false);
            dlg.setMessage("Please wait...");
            dlg.show();
        }

        @Override
        protected String doInBackground(String... params)
        {
            String result = sc.NewServiceCall(URLConstants.profileDataURL, jsonData);
            return result;
        }

        protected void onPostExecute(String result)
        {
            dlg.dismiss();
            if (result != null)
            {
                try
                {
                    JSONObject jobj = new JSONObject(result);
                    String statusCode = jobj.getString("statusCode");

                    if (statusCode.equalsIgnoreCase("1001"))
                    {
                        String response = jobj.getString("response");
                        ParseData obj = new ParseData();
                        EmployeeDTO ob = obj.parseEmployeeData(response);

                        if(profilePhoto != null)
                        {
                            profilePhoto_imgvw.setImageBitmap(profilePhoto);
                        }
                        else
                        {
                            Toast.makeText(context, "Sorry! Could not fetch the Image", Toast.LENGTH_SHORT).show();
                        }

                        empId_edt.setText(String.valueOf(ob.getEmpId()));
                        name_edt.setText(String.valueOf(ob.getFirstName()));
                        mobileNo_edt.setText(String.valueOf(ob.getMobileNo()));
                        panNo_edt.setText(String.valueOf(ob.getPan()));
                        passportNo_edt.setText(String.valueOf(ob.getPassport()));
                        emailId_edt.setText(String.valueOf(ob.getMailOffice()));
                        address_edt.setText(String.valueOf(ob.getAddress()));
                    }
                    else
                    {
                        Toast.makeText(context, jobj.getString("message"), Toast.LENGTH_LONG).show();
                        System.out.println("message"+jobj.getString("message"));
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
            else
            {
                Toast.makeText(context, result, Toast.LENGTH_LONG).show();
            }
        }
    }

3 个答案:

答案 0 :(得分:0)

检查手机上是否有足够强大的互联网连接。另外,如果我没有弄错的话,onCreate方法应@Override

答案 1 :(得分:0)

您已将后端网址列为:

http://xxx.xxx.x.xx:xxxx/XXXX/downloadAction.do?empId=etc

显然,此网址只能通过您自己的网络(来自您自己的开发PC,因此也可以在您自己的PC上运行的模拟器)访问,但无法在您自己的网络外访问。

您必须公开提供后端服务,这取决于服务的内容。

您必须告诉我们有关后端服务运行位置的更多信息。它是在您自己的开发PC上运行的服务,还是由您公司中的其他人提供的服务或其他内容?

如果它在您的开发PC上运行,那么将网址更改为您的公共IP(查看http://www.whatsmyip.org/)并打开防火墙上的端口。

答案 2 :(得分:0)

确保您的手机和PC都连接到同一个WI-FI网络。