2017-01-05 9 views
-1

画像のUri(ファイルパス)を取得しようとしています。ギャラリーからImageViewに移動し、その画像を前のアクティビティ(MainActivity)の別のNetworkImageViewにも表示しています。Uriを文字列に変換すると、Uriがnullになります

Filepath変数にコードのどこからでもアクセスできるようにします。 uploadImage()にアクセスしようとしています。最初にonActivityResult()に初期化しました。 uploadImage()では、UriStringに変換しようとしていますので、MainActivityにIntentとして送り返すことができます。

私がコードを実行すると、uploadImage()NullPointerExceptionがあり、UriStringに変換します。デバッグ時に、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(); 
      } 
     } 
    } 


} 
+0

ここで、 'filePath'に値を代入していますか? –

+1

'Uri filePath = data.getData();'を 'filePath = data.getData();'に変更します。 –

答えて

0

宣言Uri filePathにグローバルとして、その後、onActivityResultUriを削除します。

1

Uri filePath = data.getData();

//この行からUriを削除します。あなたの問題を解決します。現在、ローカル変数にdata.getData()を割り当てているので、グローバルにしてからアップロード画像からアクセスしてください。

関連する問題