0

Hello I have a troublle with this error

Exception java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { dat=file:///storage/emulated/0/DCIM/Camera/IMG_20160619_152633.jpg }} to activity {paradox.galopshop/paradox.galopshop.All}: java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference 

I want get picture from gallery in mobile phone. I select one foto and see it in imageview..

I don't know whats it thise problem because in simulator (genymotion) everything okay

protected  void onImageViewClick(){
  //  ImageView imageView=(ImageView)findViewById(R.id.imageView2);
    TextView tw=(TextView)findViewById(R.id.addimage);
    tw.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent();
            i.setType("image/*");
            i.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(i, "Select Picture"),SELECT_PICTURE );

        }
    });

}

protected  void onActivityResult(int requestCode, int resultCode, Intent data){
    if(resultCode==RESULT_OK){
        if(requestCode==SELECT_PICTURE){
            Uri selectedImageUri = data.getData();
            if (null != selectedImageUri) {
                // Get the path from the Uri
                String path = getPathFromURI(selectedImageUri);
                Log.i("IMAGE PATH TAG", "Image Path : " + path);
                // Set the image in ImageView
                ImageView imageView=(ImageView)findViewById(R.id.imageView2);
                imageView.setImageURI(selectedImageUri);

                TextView tw=(TextView)findViewById(R.id.addimage);
                tw.setText("Načítané");



            }
        }
    }
}

private String getPathFromURI(Uri contentUri) {
    String res = null;
    String[] proj = {MediaStore.Images.Media.DATA};
    Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
    if (cursor.moveToFirst()) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        res = cursor.getString(column_index);
    }
    cursor.close();
    return res;
}

My manifest

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:windowSoftInputMode="adjustResize">
    <activity android:name=".All">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Thanks for ideas..

///// UPDATE

protected  void onImageViewClick(){
  //  ImageView imageView=(ImageView)findViewById(R.id.imageView2);
    TextView tw=(TextView)findViewById(R.id.addimage);
    tw.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          /*  Intent i = new Intent();
            i.setType("image/*");
            i.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(i, "Select Picture"),SELECT_PICTURE ); */

            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("*/*");
            startActivityForResult(intent, UPLOAD_RESULT_CODE);

        }
    });

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == All.RESULT_OK) {
        // get image from intent
        InputStream inputStream = null;
        try {
            inputStream = getApplication().getContentResolver().openInputStream(data.getData());

        byte[] buffer = new byte[inputStream.available()];
        inputStream.read(buffer);
        inputStream.close();
        // Here you can decode buffer to a bitmap and show it in your image view
            Bitmap bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);
            ImageView imageView=(ImageView)findViewById(R.id.imageView2);
            Toast.makeText(this, bitmap.toString(), Toast.LENGTH_SHORT).show();
            imageView.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
user7575308
  • 81
  • 4
  • 12

1 Answers1

0

The problem is that you're getting a null cursor in getPathFromURI() here you can find why it can be null.

UPDATE

Start a pick intent

void uploadImage() { 
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*"); 
    startActivityForResult(intent, UPLOAD_RESULT_CODE); 
}

After this you can get image file from result like this

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == MainActivity.RESULT_OK) { 
         // get image from intent 
        InputStream inputStream = mActivity.getContentResolver().openInputStream(data.getData()); 
        byte[] buffer = new byte[inputStream.available()]; 
        inputStream.read(buffer); 
        inputStream.close();
        // Here you can decode buffer to a bitmap and show it in your image view
    }
}
Community
  • 1
  • 1
Eduard Albu
  • 301
  • 1
  • 8