刷新时图像未更新

时间:2016-03-11 05:14:28

标签: java android

新学员在这里。所以我刚开始探索android。更具体地说是图像。现在我正在尝试毕加索图书馆。我的问题是当我使用毕加索时,我上传到数据库的新图像没有在imageView(2.PreviewImage.java)上更新。提前帮助和tqvm。仅供参考。将图像上传到数据库时没有问题。

1.MainActivity.java

EditText editText;
Button button;

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

    editText = (EditText)findViewById(R.id.etEmail);
    button = (Button)findViewById(R.id.btnSendSp);
    button.setOnClickListener(this);
}

public void saveSpEmail(){
    SharedPreferences preferences = getSharedPreferences("PassingEmail", MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("passEmail", editText.getText().toString());
    editor.commit();
}

@Override
public void onClick(View v) {
    Intent intent = new Intent(this, PreviewImage.class);
    //intent.putExtra("email", editText.getText().toString());
    saveSpEmail();
    startActivity(intent);
}

2.PreviewImage.java

Button buttonCoProfileUploadClinicImage;

TextView textView;

private ImageView imageViewCoProfilePageClinicImage;
private Bitmap bitmap;
String imagePath;

String receiveSpEmail;

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

    buttonCoProfileUploadClinicImage = (Button)findViewById(R.id.btnCoProfileUploadImage);
    buttonCoProfileUploadClinicImage.setOnClickListener(this);
    textView = (TextView)findViewById(R.id.tvEmail);

    receiveSpEmail();
    //Intent intent = getIntent();
    //receiveSpEmail = intent.getStringExtra("email");
    textView.setText(receiveSpEmail);

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    imageViewCoProfilePageClinicImage = (ImageView) findViewById(R.id.ivCoProfileClinicImage);
    getCoImageProfile();

}

public void receiveSpEmail(){
    SharedPreferences preferences = getSharedPreferences("PassingEmail", MODE_PRIVATE);
    receiveSpEmail = preferences.getString("passEmail", "..");
}

//added for retrieve image - 13/2/16
private void getCoImageProfile() {

    String url = ClinicOwnerGetImage.DATA_URL+receiveSpEmail.toString().trim();

    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            showImageJSON(response);
            bitmap = getBitmapFromUrl(imagePath);
            //imageViewCoProfilePageClinicImage.setImageBitmap(bitmap);

            Picasso.with(PreviewImage.this)
                    .load(imagePath)
                    //.placeholder(R.drawable.placeholder)   // optional
                    // .error(R.drawable.error)      // optional
                    .resize(400, 400)                        // optional
                    .into(imageViewCoProfilePageClinicImage);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(PreviewImage.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}
private void showImageJSON(String response){
    String image="";

    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(ClinicOwnerGetImage.JSON_ARRAY);
        JSONObject employeeData = result.getJSONObject(0);
        image = employeeData.getString(ClinicOwnerGetImage.KEY_CLINIC_IMAGE);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    imagePath = image;
    Toast.makeText(this, imagePath, Toast.LENGTH_LONG).show();

}

public Bitmap getBitmapFromUrl(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
//end of retrieve image


@Override
public void onClick(View v) {
    Intent CoUploadImage = new Intent(this, ClinicOwnerUploadImage.class);
    startActivity(CoUploadImage);
}

3.UploadImage.java

public class ClinicOwnerUploadImage extends AppCompatActivity implements View.OnClickListener{

private Button buttonCoChooseImage, buttonCoUploadImage;

private ImageView imageViewCoProfileImage;

public static final String KEY_IMAGE = "clinicimage";
public static final String KEY_EMAIL = "email";
public static final String UPLOAD_URL = "http://";
private int PICK_IMAGE_REQUEST = 1;

private Bitmap bitmap;
String receiveSpEmail;

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

    buttonCoChooseImage = (Button)findViewById(R.id.btnCoChooseImage);
    buttonCoChooseImage.setOnClickListener(this);

    buttonCoUploadImage = (Button)findViewById(R.id.btnCoUploadImage);
    buttonCoUploadImage.setOnClickListener(this);

    imageViewCoProfileImage = (ImageView)findViewById(R.id.ivCoUploadImage);

    receiveSpEmail();

}

public void receiveSpEmail(){
    SharedPreferences preferences = getSharedPreferences("PassingEmail", MODE_PRIVATE);
    receiveSpEmail = preferences.getString("passEmail", "..");
}

private void showFileChooser() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
        Uri filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            Picasso.with(getApplicationContext())
                    .load(filePath)
                            //.placeholder(R.drawable.placeholder)   // optional
                            // .error(R.drawable.error)      // optional
                    .resize(400, 400)                        // optional
                    .into(imageViewCoProfileImage);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public String getStringImage(Bitmap bmp){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 25, baos);
    byte[] imageBytes = baos.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
}

public void uploadImage(){
    final String email = receiveSpEmail.toString().trim();
    final String clinicimage = getStringImage(bitmap);
    class UploadImage extends AsyncTask<Void,Void,String> {
        ProgressDialog loading;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(ClinicOwnerUploadImage.this,"Please wait...","uploading",false,false);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            Toast.makeText(ClinicOwnerUploadImage.this, s, Toast.LENGTH_LONG).show();
        }

        @Override
        protected String doInBackground(Void... params) {
            CoUploadImageRequestHandler rh = new CoUploadImageRequestHandler();
            HashMap<String,String> param = new HashMap<String,String>();
            param.put(KEY_EMAIL,email);
            param.put(KEY_IMAGE,clinicimage);
            String result = rh.sendPostRequest(UPLOAD_URL, param);
            return result;
        }
    }
    UploadImage u = new UploadImage();
    u.execute();
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.btnCoChooseImage:
            showFileChooser();
            break;

        case R.id.btnCoUploadImage:
            uploadImage();
            Intent CoProfilePage = new Intent(this, PreviewImage.class);
            startActivity(CoProfilePage);
            break;
    }
}

1 个答案:

答案 0 :(得分:0)

PreviewImage.javaonResponse方法替换

Picasso.with(PreviewImage.this)
  .load(imagePath)
  //.placeholder(R.drawable.placeholder)   // optional
  //.error(R.drawable.error)      // optional
  .resize(400, 400)                        // optional
  .into(imageViewCoProfilePageClinicImage);

runOnUiThread(new Runnable() {
           @Override
           public void run() {
                   Picasso.with(PreviewImage.this)
                  .load(imagePath)
                 //.placeholder(R.drawable.placeholder)   // optional
                 //.error(R.drawable.error)      // optional
                  .resize(400, 400)                        // optional
                  .into(imageViewCoProfilePageClinicImage);
           }
       });

提及有问题的错误。发布您的错误日志。