Ive designed a simple fragment to take a Picture and then load the corresponding picture on an ImageView on the screen using a File Provider. Im currently getting a null pointer exception when accessing it although it is there and under the correct headings. I have checked all my settings and made sure they correspond but my application is not able to find my File Provider. My code below
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.content.FileProvider;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.io.File;
import java.util.Date;
import android.net.Uri;
import android.widget.ImageView;
import java.io.IOException;
import java.text.SimpleDateFormat;
/**
* A simple {@link Fragment} subclass.
* Use the {@link StudentID#newInstance} factory method to
* create an instance of this fragment.
*/
public class StudentID extends Fragment {
String mCurrentPhotoPath;
Uri mPhotoURI;
static final int REQUEST_IMAGE_CAPTURE = 1;
static final int REQUEST_TAKE_PHOTO = 1;
ImageView picView;
FloatingActionButton button;
public StudentID() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment StudentID.
*/
// TODO: Rename and change types and number of parameters
public static StudentID newInstance() {
StudentID fragment = new StudentID();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_student_id, container, false);
picView = (ImageView)view.findViewById(R.id.idPic);
button = (FloatingActionButton)view.findViewById(R.id.picButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
takePicture();
}
});
//updateImage();
return view;
}
public void takePicture() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getContext().getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
}
// Continue only if the File was successfully created
if (photoFile != null) {
mPhotoURI = FileProvider.getUriForFile(getContext(),
"com.tble.brgo.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == getActivity().RESULT_OK) {
picView.setImageURI(mPhotoURI);
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
}
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tble.brgo">
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.tble.brgo.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths.xml"></meta-data>
</provider>
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18"/>
FileProvider:
<?xml version="1.0" encoding="utf-8"?>
<paths xmnls="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/com.tble.brgo.BRGO/files/Pictures" />
</paths>