0

Im trying to pick image from gallery and download to imageView.I picked image but that doesnt puts in imageView.in onActivity result method at cursor.moveToFirst I have yellow hint:"cursor.moveToFirst() may produce java.lang.NullPointerException.What Im did wrong?

public class Tagging_screen extends Fragment {
    String URL;
    Button btndownload;
    ImageView imageView;
    EditText editText;
    Button btngallery;
    private static final int RESULT_LOAD_IMAGE = 1;
    private static final int RESULT_OK = 2;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.tagging_screen, null);
         imageView =(ImageView)v.findViewById(R.id.imageView);
         editText = (EditText)v.findViewById(R.id.editText_download);
         btndownload = (Button)v.findViewById(R.id.btn_download);
        btngallery = (Button)v.findViewById(R.id.button_gallery);
        return v;

    }
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        btndownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                URL = editText.getText().toString();
                Picasso.with(getActivity()).load(URL).into(imageView);
            }
        });
        btngallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
    }

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

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getActivity().getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);

            cursor.moveToFirst();

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


            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }


    }
}
Brendon
  • 1,368
  • 1
  • 12
  • 28
Rost
  • 53
  • 8
  • 1
    Possible duplicate of [Method invocation may produce java NullpointerException](http://stackoverflow.com/questions/27629311/method-invocation-may-produce-java-nullpointerexception) –  Jan 20 '16 at 06:44
  • So you do not actually have a null pointer exception, there is just the warning? And the main problem seems to be that you can't get the image via the URI you are handed in onActivityResult()? – Bö macht Blau Jan 20 '16 at 06:44
  • yes,can you help me? – Rost Jan 20 '16 at 06:47
  • Look at this [answer](http://stackoverflow.com/a/20866998/5015207) for a good explanation of how to write your query. And you should also *read the link in the first comment* and always check whether your query has a result before trying to access the result. – Bö macht Blau Jan 20 '16 at 07:01

0 Answers0