Tengo una actividad donde tengo dos botones: subir imagen y tomar foto Al presionar subir imagen se abre la galería de mi dispositivo y al seleccionarla se muestra una previa en mi imageview pero el boton tomar foto ejecuta la camara del dispositivo pero cuando la selecciono y al presionar aceptar no se muestra la imagen previa ni se guarda la imagen y tampoco tengo error en mi logcat:
Este es el metodo onActivityResult():
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==PHOTO_CONST && resultCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null){
Uri uri = Uri.parse(absolutePath);
img.setImageURI(uri);
//Toast.makeText(this, "No se pudo subir la imagen", Toast.LENGTH_SHORT).show();
}else{
if (data != null) {
imgUri = data.getData();
img.setImageURI(imgUri);
imgURI = imgUri;
Glide.with(this).load(imgUri).into(img);
Toast.makeText(this, "Se subió la imagen ", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "No se pudo subir la imagen", Toast.LENGTH_SHORT).show();
}
}
}
Aqui cree la ruta donde se almacena la imagen:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android" >
<external-path
name="mImages"
path="Android/data/com.example.ftsapp/files/pictures"/>
</paths>
Aqui el metodo tomarFoto();
public void tomarFoto(){
Intent tomarFotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (tomarFotoIntent.resolveActivity(getPackageManager())!=null){
File photoFile = null;
try{
photoFile = crearFotoFile();
}catch (Exception e){
e.printStackTrace();
}
if (photoFile!=null){
Uri photoUri = FileProvider.getUriForFile(getApplicationContext(),"com.example.ftsapp",photoFile);
tomarFotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(tomarFotoIntent, PHOTO_CONST);
}
}
startActivity(tomarFotoIntent);
}
También el método para crear el archivo de la foto;
private File crearFotoFile() throws IOException {
String timestamp = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
String imageFileName = "imagen "+timestamp;
File storageFile = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File photoFile = File.createTempFile(
imageFileName,
".jpg",
storageFile
);
absolutePath = photoFile.getAbsolutePath();
return photoFile;
}
No esta devolviendo la imagen ni ningún dato.
Edicion: Ahora estoy obtenido este error en mi Logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ftsapp, PID: 1253
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at androidx.core.content.FileProvider.parsePathStrategy(FileProvider.java:605)
at androidx.core.content.FileProvider.getPathStrategy(FileProvider.java:579)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:417)
at com.example.ftsapp.MainActivity8.tomarFoto(MainActivity8.java:229)
at com.example.ftsapp.MainActivity8.onClick(MainActivity8.java:131)
at android.view.View.performClick(View.java:7140)
at android.view.View.performClickInternal(View.java:7117)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27351)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Esto luego de cambiar mi metodo crearFotoFile por createImageFile():
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "Backup_" + 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
currentPhotoPath = image.getAbsolutePath();
return image;
}
Anexo Manifest:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.hardware.camera" android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_panoramica_pv_02"
android:label="@string/app_name_long"
android:roundIcon="@drawable/ic_pv"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ListaAct"></activity>
<activity android:name=".MainActivity3" />
<activity android:name=".ListaGastos" />
<activity android:name=".MainActivity9" />
<activity
android:name=".MainActivity8"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar" />
<activity
android:name=".MainActivity7"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar" />
<activity
android:name=".MainActivity6"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar" />
<activity
android:name=".MainActivity5"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar" />
<activity
android:name=".MainActivity4"
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar" />
<activity
android:name=".MainActivity2"
android:theme="@style/AppTheme_FTS" />
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme_FTS">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.ftsapp"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths">
</meta-data>
</provider>
</application>
</manifest>