0

Hello guys I'm trying to save an image that is produced on the app from android studio . the picture is saved on android 7 but on android 10 it's not saved into the gallery 0. I've tried multiple solution.

here the code `public void SaveClick(View view){ ConstraintLayout savingLayout =(ConstraintLayout) findViewById(R.id.layoutavatar); File file = saveBitMap(fut.this, savingLayout); AlertDialog.Builder builder = new AlertDialog.Builder(fut.this);

    builder.setCancelable(true);
    builder.setTitle("AVATAR HAS BEEN SAVED");
    builder.setMessage("YOUR AVATAR HAS BEEN SAVED SUCCESSFULLY TO YOUR IMAGES DIRECTORY");


    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            alertTextView.setVisibility(View.VISIBLE);
        }
    });
    builder.show();
}

private File saveBitMap(Context context, View drawView){

    FileOutputStream outputStream = null;
    File sdCard = Environment.getExternalStorageDirectory();
    File pictureFileDir = new File(sdCard.getAbsolutePath() + "/AMONGUS-AVATAR");
    pictureFileDir.mkdir();

    String filename = pictureFileDir.getPath() +File.separator+ System.currentTimeMillis()+".jpg";
    File pictureFile = new File(filename);
    Bitmap bitmap =getBitmapFromView(drawView);
    try {
        pictureFile.createNewFile();
        FileOutputStream oStream = new FileOutputStream(pictureFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, oStream);
        oStream.flush();
        oStream.close();
    } catch (IOException e) {
        e.printStackTrace();
        Log.i("TAG", "There was an issue saving the image.");
    }
    scanGallery( context,pictureFile.getAbsolutePath());
    return pictureFile;
}

//create bitmap from view and returns it
private Bitmap getBitmapFromView(View view) {
    //Define a bitmap with the same size as the view
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
    //Bind a canvas to it
    Canvas canvas = new Canvas(returnedBitmap);
    //Get the view's background
    Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null) {
        //has background drawable, then draw it on the canvas
        bgDrawable.draw(canvas);
    }   else{
        //does not have background drawable, then draw white background on the canvas
        canvas.drawColor(Color.TRANSPARENT);
    }
    // draw the view on the canvas
    view.draw(canvas);
    //return the bitmap
    return returnedBitmap;
}


// used for scanning gallery
private void scanGallery(Context cntx, String path) {
    try {
        MediaScannerConnection.scanFile(cntx, new String[]{path}, null, new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        Log.i("TAG", "There was an issue scanning gallery.");
    }


}

My android Manifest :

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_INTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:hardwareAccelerated="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:requestLegacyExternalStorage="true"
    android:largeHeap="true"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme">

    <activity android:name=".Choosename"></activity>
    <activity android:name=".privacypolicy" />
    <activity android:name=".disclaimerfut" />
    <activity android:name=".fut" />
    <activity android:name=".Choosemenu" />
    <activity android:name=".Mainmenu" />
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.example.android.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>
</application>
  • You cannot create a file in root of external storage in 10+. Choose a different directory. For instance have a look at getExternalFilesDir(). – blackapps Nov 29 '20 at 09:40
  • But before that you cannot create that directory with pictureFileDir.mkdir(); – blackapps Nov 29 '20 at 09:43

1 Answers1

0

when i changed the graddle file the targetSdk version from 30 to 29 it worked any suggestions defaultConfig { applicationId 'com.ybq.fut21cardbuilder' minSdkVersion 16 targetSdkVersion 29 versionCode 1 versionName "1.0" vectorDrawables.useSupportLibrary = true

  • `android:requestLegacyExternalStorage` doesn't work on android 11 devices if your app is targetting API 30. see https://stackoverflow.com/q/63364476/9241978 – Pawel Nov 29 '20 at 00:45