2016-05-16 80 views
0

我从sd卡位置获取文件从活动结果它给uri。但从该uri我无法获取文件路径可以任何一个帮助我在this.i使用游标索引变为null。获取内容uri的活动结果无法获取文件路径

+0

可能没有文件路径。 [这不是你如何使用'Uri'](https://commonsware.com/blog/2016/03/15/how-consume-content-uri.html)。除此之外,请提供[mcve]。 – CommonsWare

回答

0
you can use this to get the uri path it will work for me: 



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


     // display it in image view 
     if (Gallery) { 
      if (data != null && resultCode == getActivity().RESULT_OK) { 
       // HelperJava help = new HelperJava(); 


       try { 
        // When an Image is picked 
        if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK 
          && null != data) { 
         // Get the Image from data 

         Uri selectedImage = data.getData(); 
         String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

         // Get the cursor 
         Cursor cursor = getActivity().getContentResolver().query(selectedImage, 
           filePathColumn, null, null, null); 
         // Move to first row 
         cursor.moveToFirst(); 

         int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
         imgDecodableString = cursor.getString(columnIndex); 
         cursor.close(); 

         // Set the Image in ImageView after decoding the String 

         if (cover_photo) { 
          mImageView_cover.setImageBitmap(BitmapFactory 
            .decodeFile(imgDecodableString)); 
          Hit_Upload_Cover_Image_Service(); 
         } 

         if (profile_pic) { 
          Profile_image.setImageBitmap(BitmapFactory 
            .decodeFile(imgDecodableString)); 
          Log.d("Camera_Profile", "camera profile_image"); 
          Hit_Upload_Image_Gallery_Service(); 

         } 


//      if (Gallery) { 
//       mButton_Uload_Img.setVisibility(View.VISIBLE); 
//      } 


        } else { 
         Toast.makeText(getActivity(), "You haven't picked Image", 
           Toast.LENGTH_LONG).show(); 
        } 
       } catch (Exception e) { 
        Toast.makeText(getActivity(), "Something went wrong", Toast.LENGTH_LONG) 
          .show(); 
       } 
      } 
     } else if (Camera) { 

      if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) { 
       if (resultCode == RESULT_OK) { 
        // successfully captured the image 


        previewCapturedImage(); 


       } else if (resultCode == RESULT_CANCELED) { 
        // user cancelled Image capture 
        Toast.makeText(getActivity(), 
          "cancelled image capture", Toast.LENGTH_SHORT) 
          .show(); 
       } else { 
        // failed to capture image 
//     Toast.makeText(getApplicationContext(), 
//       "Sorry! Failed to capture image", Toast.LENGTH_SHORT) 
//       .show(); 
       } 
      } 
     } 


    }