[英]Image is showing in emulator but not in the android phone
在這里,我試圖從URL提取圖像。 圖像在模擬器中的加載良好,但在Android設備中卻無法加載。 吐司消息將顯示在設備中。 我嘗試了提到的解決方案,但效果不佳。
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();
}
}
}
檢查手機上的互聯網連接是否足夠牢固。 另外,如果我沒記錯的話, onCreate
方法應該@Override
。
您已將后端網址列出為:
http://xxx.xxx.x.xx:xxxx / XXXX / downloadAction.do?empId = etc。
顯然,只能從您自己的網絡(從您自己的開發PC以及因此在您自己的PC上運行的仿真器)訪問此url,但無法在您自己的網絡外部訪問。
您將必須公開后端服務,這取決於該服務是什么。
您將不得不告訴我們有關后端服務在何處運行的更多信息。 它是在您自己的開發PC上運行的服務,還是由公司中的其他人或其他人提供的服務?
如果它在您的開發PC上運行,則將URL更改為您的公共ip(在http://www.whatsmyip.org/上查找該URL)並打開防火牆上的端口。
確保您的手機和PC都連接到相同的WI-FI網絡。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.