如何从String实例化Uri?

时间:2016-01-16 18:04:24

标签: java android hashmap base64

在发送到服务器之前将Uri图片转换为Base64 String的正确方法是什么?

public void update( final String claimType,  final String Amount, final String Description, final String imageUri)
    {
           class updateImageAndText extends AsyncTask<Void,Void,String>{
              // ProgressDialog loading;
               @Override
               protected void onPreExecute() {
                   super.onPreExecute();
                  // loading = ProgressDialog.show(Edit_Staff.this,"Updating...","Wait...",false,false);
               }

               @Override
               protected void onPostExecute(String s) {
                   super.onPostExecute(s);
                  // loading.dismiss();
                   Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
                   try {
                       Intent returnIntent = new Intent();
                       returnIntent.putExtra("ClaimType", claimType);
                       returnIntent.putExtra("Amount", Amount);
                       returnIntent.putExtra("Description", Description);
                       returnIntent.putExtra("photo", imageUri);
                       setResult(Activity.RESULT_OK, returnIntent);
                       finish();
                   }catch(Exception e)
                   {

                   }
               }

               @Override
               protected String doInBackground(Void... params) {
                   HashMap<String,String> hashMap = new HashMap<>();
                   hashMap.put(Configs.KEY_ID, String.valueOf(ID));
                   Log.e("ID", ID + "");
                   hashMap.put(Configs.KEY_TYPE, claimType);
                   hashMap.put(Configs.KEY_AMOUNT, Amount);
                   hashMap.put(Configs.KEY_DESCRIPTION, Description);
                   if(imageUri != null){
                       Log.d("log", "photo " + imageUri);
                       hashMap.put(Configs.KEY_IMAGE,getStringImage(imageUri)); // error
                   }else{
                       Log.d("log", "photo is null " );
                   }
                   RequestHandler rh = new RequestHandler();
                   String s = rh.sendPostRequest(Configs.URL_UPDATEDE_IMAGE_TEXT,hashMap);
                   return s;
               }
           }

        updateImageAndText ue = new updateImageAndText();
        ue.execute();
    }

 public String getStringImage(Uri imgUri) {

        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imgUri);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] imageBytes = baos.toByteArray();
            String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
            return encodedImage;
        } catch (Exception e) {
        }

        return "";
    }

错误

Error:incompatible types: String cannot be converted to Uri

1 个答案:

答案 0 :(得分:0)

在更新中,您将imageUri作为字符串传递:

public void update( final String claimType,  final String Amount, final String Description, final String imageUri)  

hashMap.put(Configs.KEY_IMAGE,getStringImage(imageUri)); // error because imageUri is a String

但是你的方法期望Uri不是String:

public String getStringImage(Uri imgUri){...}