У меня есть приложение, в котором я пытаюсь захватить пользовательские изображения с камеры, а также из галереи. Захват изображения работает нормально, и изображение отображается в ImageView
, но при перезапуске приложения изображение исчезает, как если бы оно не было сохранено.
public class MainActivity extends AppCompatActivity {
ImageView imageView;
int REQUEST_CAMERA =1;
int SELECT_FILE = 0;
public String photoFileName;
File photoFile = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
imageView = findViewById(R.id.imageView);
final FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
selectImage();
}
});
}
public void selectImage(){
final CharSequence [] items = {"Camera","Gallery","Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Add Image");
builder.setItems(items, new DialogInterface.OnClickListener() {
@SuppressLint("IntentReset")
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (items[i].equals("Camera")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,REQUEST_CAMERA);
try {
photoFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this,
"Photo file can't be created, please try again",
Toast.LENGTH_SHORT).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
"com.example.android.fileprovider",
photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
}
}else if (items[i].equals("Gallery")){
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent.createChooser(intent,"Select File"),SELECT_FILE);
galleryAddPic();
}else if (items[i].equals("Cancel")){
dialogInterface.dismiss();
}
}
});
builder.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CAMERA && resultCode == Activity.RESULT_OK){
Bundle bundle = null;
if (data != null) {
bundle = data.getExtras();
}
Bitmap bitmap = null;
if (bundle != null) {
bitmap = (Bitmap) bundle.get("data");
}
imageView.setImageBitmap(bitmap);
}else if (requestCode == SELECT_FILE){
Uri uri = data.getData();
imageView.setImageURI(uri);
}
}
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(String.valueOf(photoFile));
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
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 = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
photoFileName = image.getAbsolutePath();
return image;
}
}