-1

am developing an app to upload images to my server via gallery and camera My problem is I get a NullPointerException on some of my code and I have NO idea of how to fix this. the error occurs in my activity that calls the camera and uploads the image Other Questions on SO didn't help me

According to the log, it is this line

cursor.moveToFirst();

as well as

photo = (Bitmap) data.getExtras().get("data");

the log also gives the error

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.smartpractice.myapplication/com.smartpractice.myapplication.CameraActivity}: java.lang.NullPointerException: uri

Attaching the code now

Camera Activity

public class CameraActivity extends Activity {

Button btpic, btnup;
private Uri fileUri;
String picturePath;
Uri selectedImage;
Bitmap photo;
String ba1;
public static String URL = "https://www.smartpractice.co.za/files-upload-ruben.asp?MyForm=Yes";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

    btpic = (Button) findViewById(R.id.cpic);
    btpic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            clickpic();
        }
    });

    btnup = (Button) findViewById(R.id.up);
    btnup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            upload();
        }
    });
}

private void upload() {
    // Image location URL
    Log.e("path", "----------------" + picturePath);

    // Image
    Bitmap bm = BitmapFactory.decodeFile(picturePath);
    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 90, bao);
    byte[] ba = bao.toByteArray();
    ba1 = Base64.encodeToString(ba, Base64.NO_WRAP);

    Log.e("base64", "-----" + ba1);

    // Upload image to server
    new uploadToServer().execute();

}

private void clickpic() {
    // Check Camera
    if (getApplicationContext().getPackageManager().hasSystemFeature(
            PackageManager.FEATURE_CAMERA)) {
        // Open default camera
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

        // start the image capture Intent
        startActivityForResult(intent, 100);

    } else {
        Toast.makeText(getApplication(), "Camera not supported", Toast.LENGTH_LONG).show();
    }
}

Error occurs here

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 100 && resultCode == RESULT_OK) {

        selectedImage = data.getData();
        **photo = (Bitmap) data.getExtras().get("data");**

            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            **cursor.moveToFirst();**

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

            Bitmap photo = (Bitmap) data.getExtras().get("data");
            ImageView imageView =findViewById(R.id.Imageprev);
            imageView.setImageBitmap(photo);
        }
    }

Main Activity

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button buttonOne=findViewById(R.id.activity2btn);
    buttonOne.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {


            Intent activity2Intent=new Intent(getApplicationContext(), CameraActivity.class);
            startActivity(activity2Intent);
        }
    });

            Button buttonTwo=findViewById(R.id.activity2btn2);
            buttonTwo.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {


                    Intent activity2Intent=new Intent(getApplicationContext(), UploadActivity.class);
                    startActivity(activity2Intent);



        }
    });
}

}

MANIFEST

fest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.smartpractice.myapplication">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <activity android:name=".TimeLogActivity"/>
        <activity android:name=".CameraActivity"
            />
        <activity android:name=".UploadActivity" >
            <intent-filter>
                <action android:name="andriod.intent.action.main"/>
        </intent-filter>
        </activity>
        <activity android:name=".LoginActivity" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

After the button click, it must send me to the activity from there it opens the camera

niks
  • 173
  • 10
  • Possible duplicate of [Camera activity returning null android](https://stackoverflow.com/questions/6982184/camera-activity-returning-null-android) – Ali Ahsan Aug 06 '19 at 12:47
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – a_local_nobody Aug 06 '19 at 16:35

1 Answers1

0

Delete all the Cursor stuff. There is not supposed to be a Uri returned by ACTION_IMAGE_CAPTURE. And, even if there were a Uri, there is no requirement for the MediaStore to know about it, and the DATA column is no longer available on Android Q and higher.

Your image will be the thumbnail Bitmap from data.getExtras().get("data");.

Alternatively, provide your own Uri in EXTRA_OUTPUT (instead of the null that you are providing now), so you control where the image should be stored. This sample app shows how to use FileProvider with EXTRA_OUTPUT for this purpose.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • That solved the problem of it crashing and nullpointer BUT now after the thumbnaail is displayed and I click Upload it crashes completely – Ruben Meiring Aug 07 '19 at 08:34
  • @RubenMeiring: Use Logcat to examine the stack trace of the crash. If you cannot interpret the cause of the crash, ask a separate Stack Overflow question with your now-current code and the complete stack trace. – CommonsWare Aug 07 '19 at 10:56