title: How to write scoped storage for android 11 in java?
body: <p>Here is my app code below. I want to store images and files such as pdf to my folder path to the above 10+ version and below version. I just want to know. Is the correct way? please let me know what I have missed. It's working perfectly but I just confused with manifest permission as per google play policy for app successful update</p>
  <p>my question is the correct way for scoped storage and permissions?</p>
  <p>my manifest.xml</p>
  <pre><code>    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      package=&quot;com.example.myapplication&quot;&gt;
  
       &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
  &lt;uses-permission android:name=&quot;android.permission.WAKE_LOCK&quot; /&gt;
  &lt;uses-permission android:name=&quot;com.flendzz.licstickynote.permission.C2D_MESSAGE&quot; /&gt;
  &lt;uses-permission android:name=&quot;com.google.android.c2dm.permission.RECEIVE&quot; /&gt;
  &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; /&gt;
  &lt;uses-permission
      android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;
      android:maxSdkVersion=&quot;28&quot;
      tools:ignore=&quot;ScopedStorage&quot; /&gt;
  &lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot; /&gt;
  &lt;uses-permission android:name=&quot;android.permission.READ_PHONE_STATE&quot;
      android:maxSdkVersion=&quot;29&quot;/&gt;
  &lt;uses-permission android:name=&quot;android.permission.READ_PHONE_NUMBERS&quot;/&gt;
  &lt;uses-permission android:name=&quot;android.permission.READ_CONTACTS&quot; /&gt;
  &lt;uses-permission android:name=&quot;android.permission.RECEIVE_BOOT_COMPLETED&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_MEDIA_LOCATION&quot; /&gt;
  
      &lt;application
          android:allowBackup=&quot;true&quot;
          android:icon=&quot;@mipmap/ic_launcher&quot;
          android:label=&quot;@string/app_name&quot;
          android:requestLegacyExternalStorage=&quot;true&quot;
          android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
          android:supportsRtl=&quot;true&quot;
          android:theme=&quot;@style/Theme.MyApplication&quot;&gt;
          &lt;activity android:name=&quot;.MainActivity&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
  
                  &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  <p>activity.java</p>
  <pre><code>    private void SaveImageaBOVE10(Bitmap bitmap) {
              OutputStream fos;
              try {
      
                  if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.Q) {
                      ContentResolver contentResolver = getContentResolver();
                      ContentValues contentValues = new ContentValues();
                      contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, &quot;image_&quot; + &quot;.jpg&quot;);
                      contentValues.put(MediaStore.MediaColumns.MIME_TYPE, &quot;image/jpg&quot;);
                      contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + File.separator + &quot;Note App&quot;);
      
                      Uri imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
                      fos = (FileOutputStream) contentResolver.openOutputStream(Objects.requireNonNull(imageUri));
                      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                      Objects.requireNonNull(fos);
      
                      Toast.makeText(this, &quot;image saved&quot;, Toast.LENGTH_SHORT).show();
      
                  } else {
      
                  
      
                          SaveImage(bitmap);
                
      
                  }
      
              } catch (Exception e) {
                  e.printStackTrace();
              }
      
          }
      
  
  //below 10 version
  
      private void SaveImage(Bitmap finalBitmap) {
      
              String root = Environment.getExternalStorageDirectory().toString();
              File myDir = new File(root + &quot;/NoteApp&quot;);
              if (!myDir.exists()) {
                  myDir.mkdirs();
              }
              Random generator = new Random();
              int n = 10000;
              n = generator.nextInt(n);
              String fname = &quot;Image-&quot; + n + &quot;.jpg&quot;;
              File file = new File(myDir, fname);
              if (file.exists())
                  file.delete();
              try {
                  FileOutputStream out = new FileOutputStream(file);
                  finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                  out.flush();
                  out.close();
      
              } catch (Exception e) {
                  e.printStackTrace();
              }
      
              Toast.makeText(this, &quot;image saved in storage&quot;, Toast.LENGTH_SHORT).show();
          }
  </code></pre>