-1

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>
Shashanth
  • 4,995
  • 7
  • 41
  • 51
  • 1
    You need to look into [runtime permissions](https://stackoverflow.com/q/32635704), and [implementing a `FileProvider`](https://stackoverflow.com/q/38200282). Also, I would mention that you do not need the `CAMERA` permission to take a photo via `Intent`, so you can just remove that particular ``. If you leave it there, you'll have to request that permission at runtime, too. And, file system access is being severely restricted on Android 10+, so you'll eventually have to adjust for that, if you plan on supporting those versions. – Mike M. Nov 21 '19 at 15:52

1 Answers1

2

Starting from Android 6, some permissions are classified as dangerous permission. Dangerous permissions require not only listing in the manifest file, but also run-time requesting.

Here is a tutorial of run-time requesting. https://developer.android.com/training/permissions/requesting.html

This is the list of all permissions, you can see if one is dangerous by its protection level. https://developer.android.com/reference/android/Manifest.permission

Jeffrey Chen
  • 1,777
  • 1
  • 18
  • 29
  • when I try the app it mark the next error: 2019-11-21 10:12:57.009 13889-13889/? E/libtrusty: tipc_connect: cannot open tipc device "/dev/trusty-ipc-dev0": No such file or directory 2019-11-21 10:12:57.009 13889-13889/? E/storageproxyd: failed (-2) to connect to storage server – marisol Nov 21 '19 at 16:42