将Uri转换为String时,Android Uri为null

时间:2017-01-05 17:19:58

标签: android android-intent uri

我正在尝试获取图片的Uri(文件路径)。我从一个图库进入ImageView,并在上一个活动(NetworkImageView)中的另一个MainActivity中显示该图像。

我希望可以从代码中的任何位置访问Filepath变量。我正试图在uploadImage()中访问它。我首先在onActivityResult()初始化它。在uploadImage(),我正在尝试将Uri转换为String,因此我可以将其作为Intent发送回MainActivity。

当我运行代码时,NullPointerException中的uploadImage()会将Uri转换为String。在调试时,我意识到Filepath变量返回null 请问任何人都可以识别代码中的错误并建议解决方案吗?

    Uri filePath;
    public void uploadImage() {

        //Showing the progress dialog
        final ProgressDialog loading = ProgressDialog.show(this, "Uploading...", "Please wait...", false, false);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String s) {
                        loading.dismiss();
                        Toast.makeText(Activity2.this, s, Toast.LENGTH_LONG).show();
                        Intent returnIntent = new Intent();

             //Getting a NullPointerException on this line
                        String str = filePath.toString();
                        returnIntent.putExtra("UPDATED_PIC", str);


                        setResult(RESULT_OK,returnIntent);
                        finish();

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        //Dismissing the progress dialog
                        loading.dismiss();
                        //Showing toast
                        Toast.makeText(Activity2.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //Converting Bitmap to String
                String image = getStringImage(bitmap);
                //Creating parameters
                Map<String, String> params = new Hashtable<String, String>();

                //Adding parameters
                params.put(KEY_IMAGE, image);


                //returning parameters
                return params;
            }
        };

        //Creating a Request Queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        //Adding request to the queue
        requestQueue.add(stringRequest);
    }


    @Override
    public 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 {
                //Getting the Bitmap from Gallery
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                //Setting the Bitmap to ImageView
                imageView.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}

2 个答案:

答案 0 :(得分:1)

Uri filePath = data.getData();

//从此行中删除Uri。它会解决你的问题。目前,您在本地变量中分配data.getData(),因此将其设为全局,然后从上传图像访问它

答案 1 :(得分:0)

Uri filePath声明为全局,然后移除Uri中的onActivityResult