java - 动态获取图像到ImageView

标签 java android mysql android-layout android-studio

What my program is currently doing , it searches in a database for an ID(inputed by the user), and based on that it displays the title, and the respective "picture.jpg"(which is just a string).

What I want to do, is I want to take the string(picture.jpg), and use that to search on the server. So in the end it should be something like:

    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();
        } }



     // }

最佳答案

轻松使用毕加索图书馆: http://square.github.io/picasso/

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

关于java - 动态获取图像到ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35405922/

相关文章:

java - BigInteger modInverse 不适用于检查为可逆的数字

java - 升级 Spring Integration 版本后出现类转换异常

java - 使控制台 Print() 按顺序显示用户的输入?

java - TextView 在应用程序中不可点击

php - 数据库模型验证

php - undefined variable 实际上被定义了吗?

java - 非静态方法调用,无需在 onCreate() 内创建对象

android - 如何在 android 中打开 kml 文件

android - 如何保持安装两个版本的应用程序?

php - : MySQL LEFT(*, 100) 和 PHP substr() 哪个更快?