I have a small camera fragment that will currently take a picture of something and then save it to the camera gallery. The idea is that from now on every time this fragment is loaded it should check to see if said Image exists it should load it, I intended to do that by saving a string version of the URI to the file in preferences. Im currently saving the original file's URI but that does not seem to work. My code below
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);
picView.setScaleType(ImageView.ScaleType.FIT_XY);
button = (FloatingActionButton)view.findViewById(R.id.picButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
takePicture();
}
});
updateImage();
return view;
}
private void updateImage(){
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
mPhotoURI = Uri.parse(sharedPref.getString("StudentID", ""));
picView.setImageURI(mPhotoURI);
}
private void takePicture() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getContext().getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
}
if (photoFile != null) {
Uri photoURI = Uri.fromFile(photoFile);
mPhotoURI = photoURI;
mCurrentPhotoPath = photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
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);
addImageToGallery(mCurrentPhotoPath,getContext());
}
}
public static void addImageToGallery(final String filePath, final Context context) {
ContentValues values = new ContentValues();
values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATA, filePath);
context.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
}
private File createImageFile() throws IOException {
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,
".jpg",
storageDir
);
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
}