java - 如何从字符串实例化 Uri?

标签 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

最佳答案

在更新中,您将 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 而不是字符串:

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

关于java - 如何从字符串实例化 Uri?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34830301/

相关文章:

c++ - std::map 索引和插入调用之间的区别

android - 如何使用 ArrayAdapter 对 Android 中添加的 HashMap 进行排序

java - GNUmakefile无法编译bison和flex程序

java - 如何在 servlet 中使用 JSONObject 创建多级 JSON 数据

java - 无状态状态在工作后是否被清除?

android - 如何在 Retrofit RxJava Android 中获取 https 状态码?

android - 如何为我的应用程序添加音乐设置?喜欢安卓游戏

java - 为什么抛出 RuntimeException 错误而不是 IOException 消息?

java - PersistenceContext 为空

Struts ScopeInterceptor 类中的 Java 线程安全问题?