Can't create a folder in Gallery, external sto

2019-07-23 19:52发布

问题:

I'm new in Android and I've follow this tutorial https://developer.android.com/training/camera/photobasics.html and I can't create folder in /storage/emulated/0/Pictures.

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 = getPublicAlbumStorageDir("FileName");
    if (storageDir ==null){
        Toast.makeText(this, "Sorry no folder available", Toast.LENGTH_LONG).show();
        return storageDir;
    }
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir /* directory */
    );
    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

Someone told me to use getPublicAlbumStorageDir() https://developer.android.com/training/data-storage/files.html#java instead of getExternalFilesDir(Environment.DIRECTORY_PICTURES)

public File getPublicAlbumStorageDir(String albumName) throws IOException {
    // Get the directory for the user's public pictures directory.
    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), albumName);
    Log.i("File", file.getAbsolutePath());
    if (!file.exists()) {
        Log.e("Error", file.getAbsolutePath());
        file.mkdirs();
        if(!file.exists()){
            Log.e("Error22", file.getAbsolutePath());
        }
        else{
            Log.i("In", "you are in");
            return file;
        }
        return null;
    }
    return file;
}

Here are my permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

I've try to found more about this issue and the only working project is on this site, but some of the actions are old.

I'm using for testing Android 8.0.0 and also Android 6.0.0.

It's working If you want to make a folder on gallery, this how you should do:

  1. Go to https://developer.android.com/training/camera/photobasics.html#TaskPath and copy permissions to your AndroidManifest.xml as adding a FileProvider inside <application> tags. Then inside res or resource, create a folder with the name xml and inside make a xml file called file_paths, in the file copy what is under FileProvider but change path to path=".".

    (Don´t forget to ask permissions to the user, if you use it for real).

  2. In the same page, above you will find private void dispatchTakePictureIntent(), copy that and static final int REQUEST_TAKE_PHOTO = 1. dispatchTakePictureIntent() is the first thing you need to call, in order to work.
  3. After that, create a String mCurrentPath and copy Private File createImageFile() from above.
  4. Next, copy public File getPublicAlbumStorageDir(String albumName) which is above this explanation.
  5. If you run it it will show the camera and you can deny or accept the photo you take. However you still have to add a little more code.

    String REQUEST_TAKE_PHOTO = 2; @Override protected void onActivity(int requestCode, int requestCode, Intent data){ if(requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK){ //Show ImageView here if you want galleryAddPic(); } super.onActivityResult(requestCode, resultCode, data); }

  6. In https://developer.android.com/training/camera/photobasics.html#TaskGallery duplicate private void galleryAddPic() and if you want to show the image taken, add this:

    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath()); imageView.setImageBitmap(bitmap);

And like so, your image should be in the FolderName you have defined.

I decided to make this small tutorial because I couldn't find the answer for this problem and in Android website is not well explain.

Huge thanks to greenapps! I really appreciate!