0

The following code neither creates the directory nor stores the photo in the directory that must be created.I do not know where the bug comes from, I'm searching the internet, but I can not find useful information. Do you see any fault?

       mImageButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            //Creamos la carpeta.
            File imageFolder = new File(Environment.getExternalStorageDirectory() + "/LibretaPolicial");
            if(!imageFolder.exists()){
                imageFolder.mkdirs();
                Toast.makeText(BorrarActivity.this, "Creado", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(BorrarActivity.this, "NOOOOO creado", Toast.LENGTH_SHORT).show();
            }



            File image = new File(imageFolder, numeroAleatorio+".jpg");
            Uri uriSavedImage = Uri.fromFile(image);

            //Le decimos al Intent que queremos guardar la imagen.
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

            //Lamzamos la aplicación de la camara
            startActivityForResult(cameraIntent, 1);
        }
    });


}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //Comprovamos que la foto se a realizado
    if (requestCode == 1 && resultCode == RESULT_OK) { //requestCode: 1 requestCode : 0
        //Creamos un bitmap con la imagen recientemente
        //almacenada en la memoria
        Bitmap bMap = BitmapFactory.decodeFile(
                getExternalStorageDirectory()+
                        "/LibretaPolicial/"+numeroAleatorio+".jpg");
        //Añadimos el bitmap al imageView para
        //mostrarlo por pantalla
        mImageView.setImageBitmap(bMap);
    }
}

I believe the permissions in the AndroidManifest well and does not show me any errors, just do not create the directory or store the photo in the directory that should have been created.

   <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Activities.SplashActivity">
            <intent-filter>
                <action
                    android:name="android.intent.action.MAIN"
                    android:screenOrientation="portrait"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".Activities.MainActivity"
            android:screenOrientation="portrait">

        </activity>
        <activity
            android:name=".Activities.BorrarActivity"
            android:screenOrientation="portrait">
        </activity>
        <activity
            android:name=".Activities.ModificarActivity"
            android:screenOrientation="portrait">
        </activity>
        <activity
            android:name=".Activities.BorrarPlacasActivity"
            android:screenOrientation="portrait">
        </activity>
        <activity
            android:name=".Activities.ModificarPlacaActivity"
            android:screenOrientation="portrait">
        </activity>
        <activity
            android:name=".Activities.ClaveActivity"
            android:screenOrientation="portrait">
        </activity>
    </application>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE">
    </uses-permission>

</manifest>
skirlappa
  • 1
  • 1

2 Answers2

0

Try using:

File imageFolder = new File(Environment.getExternalStorageDirectory(), "LibretaPolicial");
Maarten van Tjonger
  • 1,867
  • 1
  • 10
  • 15
0

I just wonder how did you call getExternalStorageDirectory() in onActivityResult withou Environment prefix?

Anyway, I did write a new code that could help you

  • I check the state of Environment.getExternalStorageState() before creating dir
  • I use File.separator instead of / for safe ( Using / may cause error)
  • I create a temp file for faster loading after that

This is the code

public static final int REQUEST_TAKE_PHOTO = 1;
private File photoCapturedFile;
public void openCamera() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    String timeStamp = System.currentTimeMillis() + ""; // You can use any name
    String fileName = "JPEG_" + timeStamp;

    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        photoCapturedFile = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES),
                fileName + ".jpg");
    } else {
        String dir = Environment.getExternalStorageDirectory() + File.separator + "myDirectory";
        //create folder
        File folder = new File(dir); //folder name
        if (!folder.exists()) {
            folder.mkdirs();
        }

        //create file
        photoCapturedFile = new File(dir, fileName + ".jpg");
    }

    Uri temUri = Uri.fromFile(photoCapturedFile);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, temUri);
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
    if (intent.resolveActivity(context.getPackageManager()) != null) {
        startActivityForResult(intent, REQUEST_TAKE_PHOTO);
    }
}

And then in onActivityResult

public void onActivityResult(int requestCode, Intent data) {
    switch (requestCode) {
        case REQUEST_TAKE_PHOTO:
            if (photoCapturedFile != null) {
                // handle photoCapturedFile.getAbsolutePath() ....
            }
            break;
    }
}
phatnhse
  • 3,870
  • 2
  • 20
  • 29