从URL数据库加载图像

时间:2015-01-15 18:51:10

标签: java android image url load

第一个代码:

`db_img = Jasonobject.getString("image");
imagelink.setText(db_img);`
  • 上面的代码是从mysql数据库中获取数据。
  • “imagelink”是textview。
  • 代码会将数据显示到textview
  • 来自mysql的
  • “image”是字符串,因为是url link。

第二段代码:

    new DownloadImageTask((ImageView) findViewById(R.id.qrimg)).
         execute("http://localhost/project/image/pic1.jpg");
  • 第二个代码是在URL上加载图像

  • 我的问题是如何将第一个代码的值传递给.execute(“HERE”)

代码: `

EditText etacode;
EditText txtName;
Button btnscanitem;


//testing
TextView imagelink;

//hidden textview 
TextView texttime;
TextView textunit;
TextView textrm;
TextView textexp;
Button btnconfrim;

ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.acode);

    btnscanitem = (Button) findViewById(R.id.btnscanitem);
    etacode = (EditText) findViewById(R.id.etacode);
    etacode.setVisibility(View.GONE);       


    //testing
    imagelink = (TextView) findViewById(R.id.imagelink);

    btnconfrim = (Button) findViewById(R.id.btnconfrim);
    btnconfrim.setVisibility(View.GONE);

    img = (ImageView) findViewById(R.id.img);
    img.setVisibility(View.GONE);
    img.setImageResource(0);

}

class task extends AsyncTask<String, String, Void>
{
private ProgressDialog progressDialog = new ProgressDialog(AcodeActivity.this);
    InputStream is = null ;
    String result = "";
    protected void onPreExecute() {
       progressDialog.setMessage("Fetching data...");
       progressDialog.show();
       progressDialog.setOnCancelListener(new OnCancelListener() {
    public void onCancel(DialogInterface arg0) {
        task.this.cancel(true);
       }
    });
     }
       @Override
    protected Void doInBackground(String... params) {
      String url_select = "http://Localhost/getproduct.php";

      HttpClient httpClient = new DefaultHttpClient();
      HttpPost httpPost = new HttpPost(url_select);

          ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();

        try {
        httpPost.setEntity(new UrlEncodedFormEntity(param));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();

        //read content
        is =  httpEntity.getContent();                  

        } catch (Exception e) {

        Log.e("log_tag", "Error in http connection "+e.toString());

        }
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = "";
        while((line=br.readLine())!=null)
        {
           sb.append(line+"\n");
        }
            is.close();
            result=sb.toString();               

                } catch (Exception e) {
                    // TODO: handle exception
                    Log.e("log_tag", "Error converting result "+e.toString());
                }

            return null;

        }
    protected void onPostExecute(Void v) {

        // Fetch data from Json database
        try {
            JSONArray Jarray = new JSONArray(result);
            for(int i=0;i<Jarray.length();i++)
            {
            JSONObject Jasonobject = null;
            Jasonobject = Jarray.getJSONObject(i);


            String apromoid = Jasonobject.getString("aid");


            String db_img="";

            if(etcode.getText().toString().equalsIgnoreCase(apromoid)) {



                db_img = Jasonobject.getString("image");
                imagelink.setText(db_img);


                String temp;
                temp = db_img;
                //String imagelink =  temp;
                //(db_img);


            break;
            }

            }
            this.progressDialog.dismiss();

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("log_tag", "Error parsing data "+e.toString());
        }
    }
    }




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.acodescanner, menu);
    return true;
}



public void onClick (View view){
    if(view.getId() == R.id.btnscanitem){
        IntentIntegrator integrator = new IntentIntegrator(this);
        integrator.initiateScan();


    }

}

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
      IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
      if (scanResult != null) {
        // handle scan result

          String acode;

          acode = scanResult.getContents();

          EditText etacode = (EditText) findViewById(R.id.etacode);

          etacode.setText(acode);

          new task().execute();



          //String textlink = getimagelink;
          new DownloadImageTask((ImageView) findViewById(R.id.qrimg))
            .execute(db_img);



      }

}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
      ImageView bmImage;

      public DownloadImageTask(ImageView bmImage) {
          this.bmImage = bmImage;
      }

      protected Bitmap doInBackground(String... urls) {
          String urldisplay = urls[0];
          Bitmap mIcon11 = null;
          try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
          } catch (Exception e) {
              Log.e("Error", e.getMessage());
              e.printStackTrace();
          }
          return mIcon11;
      }

      protected void onPostExecute(Bitmap result) {
          bmImage.setImageBitmap(result);
      }
    }

} `

0 个答案:

没有答案