I'm developing an app that takes photos. I tried it in my phone that has android 5 and it works but then i tried with two phones that have android 9 and android 7 and when I would take the photo the app is closed.
public class MainActivity extends AppCompatActivity {
final File ruta_fotos = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
final File file = new File(ruta_fotos, "DemoPicture.jpg");
private Button boton;
ImageView imagen;
public int cuenta=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imagen=(ImageView) findViewById(R.id.imagemId);
boton = (Button) findViewById(R.id.btnTomaFoto);
//Si no existe crea la carpeta donde se guardaran las fotos
file.mkdirs();
//accion para el boton
boton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String file = ruta_fotos + getCode() + ".jpg";
File mi_foto = new File( file );
try {
mi_foto.createNewFile();
} catch (IOException ex) {
Log.e("ERROR ", "Error:" + ex);
}
//
Uri uri = Uri.fromFile( mi_foto );
//Abre la camara para tomar la foto
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Guarda imagen
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//Retorna a la actividad
startActivityForResult(cameraIntent, 0);
}
});
}
@SuppressLint("SimpleDateFormat")
private String getCode()
{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date() );
String photoCode = "pic_" + date;
return photoCode;
}
public void Click(View view) {
cargarImagen();
}
private void cargarImagen (){
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/");
startActivityForResult(intent.createChooser(intent,"seleccione a la aplicacion"),10);
cuenta=1;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK && cuenta==1){
Uri path= data.getData();
imagen.setImageURI(path);
}
}
}
I tried change permissions and it didn´t work. I leave the manifest to check how I put the permissions.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tomar">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>