I have an app which takes a picture from the phone camera and stores the path as TEXT in an sqlite database. I am retrieving the string from the database and parsing it and it is not showing in the ImageView for some reason. Here is all of the relevant code.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_detail);
editTextName = (EditText) findViewById(R.id.editTextName);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextBirthdate = (EditText) findViewById(R.id.editTextBirthdate);
editTextAge = (EditText) findViewById(R.id.editTextAge);
imageViewStudentImage=(ImageView)findViewById(R.id.imageViewStudent);
_Student_Id = 0;
Intent intent = getIntent();
_Student_Id = intent.getIntExtra("student_Id", 0);
StudentRepo repo = new StudentRepo(this);
Student student;
student = repo.getStudentById(_Student_Id);
editTextBirthdate.setText(student.bd);
editTextName.setText(student.name);
editTextEmail.setText(student.email);
//Display Image Here
if (student.imagepath !=null) imageViewStudentImage.setImageURI(Uri.parse(student.imagepath));
editTextAge.setText(CalculateAge(ConvertDate(student.bd)).toString());
}
URI is file:///storage/emulated/0/Pictures/MyCameraApp/IMG_20150525_124520.jpg
Something interesting is that I cannot find the image in my Gallery app on my phone, but I can find in with my File Browser app. The image does display properly on my phone.
Here is the code that stores the URI
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
private void Save(){
if (!editTextName.getText().toString().isEmpty()) {
StudentRepo repo = new StudentRepo(this);
Student student = new Student();
student.imagepath = fileUri.toString();
if (!editTextBirthdate.getText().toString().isEmpty()) {
student.bd = editTextBirthdate.getText().toString();
} else {
student.bd = "";
}
if (!editTextEmail.getText().toString().isEmpty()) {
student.email = editTextEmail.getText().toString();
} else {
student.email = "";
}
student.name = editTextName.getText().toString();
student.student_ID = _Student_Id;
if (_Student_Id == 0) {
_Student_Id = repo.insert(student);
Toast.makeText(this, "New Student Inserted", Toast.LENGTH_SHORT).show();
} else {
repo.update(student);
Toast.makeText(this, "Student Record updated", Toast.LENGTH_SHORT).show();
}
}
}
private void TakePicture(){
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
Can someone please help me figure out why the ImageView would not display the image?
Aucun commentaire:
Enregistrer un commentaire