动态获取ImageView图像

时间:2016-02-15 09:42:06

标签: java android mysql android-layout android-studio

  

我的程序目前在做什么,它在数据库中搜索   一个ID(由用户输入),并根据它显示标题,   以及相应的" picture.jpg"(这只是一个字符串)。

     

我想做的是,我想拍摄字符串(picture.jpg),然后使用   在服务器上搜索。所以最后它应该是某种东西   像:

    http://192.168.1.254/images/picture.jpg. And it should also display the picture.


    SecondActivity.java

    This is how my whole code looks like:

    package br.exemplozxingintegration;

    import android.annotation.SuppressLint; 
import android.app.ProgressDialog; 
import android.content.ClipData; 
import android.content.ClipboardManager; 
import android.media.Image; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast;

    import com.android.volley.RequestQueue; 
import com.android.volley.Response; 
import com.android.volley.VolleyError;
 import com.android.volley.toolbox.ImageRequest;
 import com.android.volley.toolbox.StringRequest;
 import com.android.volley.toolbox.Volley;
 import com.squareup.picasso.Picasso;

    import org.json.JSONArray;
 import org.json.JSONException; 
import org.json.JSONObject;



    public class SecondActivity extends AppCompatActivity implements View.OnClickListener  {



        private EditText  pastetext;
        private ClipboardManager myClipboard;
        private ClipData myClip;
        private Button btn;
        private EditText textView1;
        private Button buttonGet;
        private TextView textViewResult;
        private ImageView ImageView1;

        private ProgressDialog loading;


              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);

                  setContentView(R.layout.activity_second);
                  myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                  pastetext = (EditText) findViewById(R.id.textView1);
                  btn = (Button)findViewById(R.id.buttonPaste);
                  btn.performClick();




                  textView1 = (EditText) findViewById(R.id.textView1);
                  buttonGet = (Button) findViewById(R.id.buttonGet);
                  textViewResult = (TextView) findViewById(R.id.textViewResult);
                  Image = (ImageView) findViewById(R.id.imageView1);

                  buttonGet.setOnClickListener(this);

              }



              @SuppressLint("NewApi")
              public void paste(View view) {
                  ClipData cp = myClipboard.getPrimaryClip();
                  ClipData.Item item = cp.getItemAt(0);
                  String text = item.getText().toString();
                  pastetext.setText(text);
                  Toast.makeText(getApplicationContext(), "Text Pasted",
                          Toast.LENGTH_SHORT).show();


              }


        private void getData() {
            String qrcode = textView1.getText().toString().trim();
            if (qrcode.equals("")) {
                Toast.makeText(this, "", Toast.LENGTH_LONG).show();
                return;
            }
            loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);

            String url = Config.DATA_URL+textView1.getText().toString().trim();

            StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    loading.dismiss();
                    showJSON(response);
                }
            },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(SecondActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                        }
                    });

            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);
        }

        private void showJSON(String response){
            String title="";
            String image = "";
            try {
                JSONObject jsonObject = new JSONObject(response);
                JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
                JSONObject androidData = result.getJSONObject(0);
                title = androidData.getString(Config.KEY_TITLE);
                image = androidData.getString(Config.KEY_IMAGE);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            textViewResult.setText("Title:\t" + title);//+"\nImagine :\t"+ image);
            //int id = getResources().getIdentifier("http://192.168.1.254/2015/380panel/uploads/images/sm/"
    + image, null, null);
            //ImageView1.setImageURI(id);
            Picasso.with(this).load("http://192.168.1.254/2015/380panel/uploads/images/sm/"
    + image).into(ImageView1);

        }

        @Override
        public void onClick(View v) {
            getData();
        } }



     // }

3 个答案:

答案 0 :(得分:0)

毕加索图书馆很容易: http://square.github.io/picasso/

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

答案 1 :(得分:0)

毕加索图书馆:http://square.github.io/picasso/

  

将此项添加到依赖项的gradle文件中:

compile 'com.squareup.picasso:picasso:2.5.2'
  

添加此库后,请尝试像这样加载图片网址。

Picasso.with(context).load("<Any Image URL>").into(imageView);

希望它会对你有所帮助!

答案 2 :(得分:0)

由于你是alreadu使用volley,所以不需要使用Picasso.Volley为你提供了一个NetworkImageView.Simply用com.android.volley.toolbox.NetworkImageView替换xml中的ImageView并在代码中使用

NetworkImageView image = (NetworkImageView)findViewById(R.id.imageView);
image.setImageUrl("your url");