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
See Question&Answers more detail:
os