The application does not work on newer versions of Android (version 9 and above)

66 Views Asked by At

This application sets the tones, but it stops working in storing the tone on the newer versions and works efficiently on the old versions. I would like your help to solve the problem

I think the storage path has been changed for the latest versions

I've tried and searched a lot and can't find a solution to this problem

This is the code

private void requestPermission () {

    if (ActivityCompat.shouldShowRequestPermissionRationale ( this , android.Manifest.permission.WRITE_EXTERNAL_STORAGE )) {
        Intent intent = new Intent ( );
        intent.setAction ( Settings.ACTION_APPLICATION_DETAILS_SETTINGS );
        Uri uri = Uri.fromParts ( "package" , getPackageName ( ) , null );
        intent.setData ( uri );
        startActivity ( intent );
    } else {
        ActivityCompat.requestPermissions ( this , new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE} , REQUEST_CODE );
    }
}

@SuppressLint("ObsoleteSdkInt")
@Override
public void onRequestPermissionsResult ( int requestCode , @NonNull String[] permissions , @NonNull int[] grantResults ) {
    super.onRequestPermissionsResult ( requestCode , permissions , grantResults );
    if (requestCode == REQUEST_CODE) {
        if (grantResults.length > 0 && grantResults[ 0 ] == PackageManager.PERMISSION_GRANTED) {


            if (Build.VERSION.SDK_INT >= 23) {
                if (! Settings.System.canWrite ( this )) {
                    Intent intent = new Intent ( Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION )
                            .setData ( Uri.parse ( "package:" + getPackageName ( ) ) )
                            .addFlags ( Intent.FLAG_ACTIVITY_NEW_TASK );
                    startActivity ( intent );
                }
            }
        }
    }
}


@SuppressLint("Recycle")
private void set_tone ( String N1 , String N2 ) {
    AssetFileDescriptor openAssetFileDescriptor;
    (( AudioManager ) getSystemService ( AUDIO_SERVICE )).setRingerMode ( 2 );
    File file = new File ( Environment.getExternalStoragePublicDirectory ( Environment.DIRECTORY_MUSIC ) + "/AndRody" , N2 );
    if (! file.getParentFile ( ).exists ( )) {

        file.getParentFile ( ).mkdirs ( );

    }
    if (! file.exists ( )) {
        try {
            file.createNewFile ( );
        } catch (IOException e) {
            e.printStackTrace ( );
        }
    }
    Uri parse = Uri.parse ( "android.resource://" + getPackageName ( ) + "/raw/" + N2 );
    ContentResolver contentResolver = getContentResolver ( );
    try {
        openAssetFileDescriptor = contentResolver.openAssetFileDescriptor ( parse , "r" );
    } catch (FileNotFoundException e2) {
        openAssetFileDescriptor = null;
    }
    try {
        byte[] bArr = new byte[1024];
        assert openAssetFileDescriptor != null;
        FileInputStream createInputStream = openAssetFileDescriptor.createInputStream ( );
        FileOutputStream fileOutputStream = new FileOutputStream ( file );
        for (int read = createInputStream.read ( bArr ); read != - 1; read = createInputStream.read ( bArr )) {
            fileOutputStream.write ( bArr , 0 , read );
        }
        fileOutputStream.close ( );
    } catch (IOException e3) {
        e3.printStackTrace ( );
    }

    ContentValues contentValues = new ContentValues ( );
    contentValues.put ( "_data" , file.getAbsolutePath ( ) );
    contentValues.put ( "title" , N1 );
    contentValues.put ( "mime_type" , "audio/mpeg" );
    contentValues.put ( "_size" , file.length () );
    contentValues.put ( "is_ringtone" , Boolean.TRUE );

    try {
        Toast.makeText ( this , new StringBuilder ( ).append ( "تم تعيين النغمة بنجاح" ) , Toast.LENGTH_LONG ).show ( );
        RingtoneManager.setActualDefaultRingtoneUri ( getBaseContext ( ) , 1, contentResolver.insert ( MediaStore.Audio.Media.getContentUriForPath ( file.getAbsolutePath ( ) ) , contentValues ) );
    } catch (Throwable th) {
        Toast.makeText ( this , new StringBuilder ( ).append ( "يوجد خلل ما" ) , Toast.LENGTH_LONG ).show ( );
    }


}

I think the change should be in this code

ContentValues contentValues = new ContentValues ( );
contentValues.put ( "_data" , file.getAbsolutePath ( ) );
contentValues.put ( "title" , N1 );
contentValues.put ( "mime_type" , "audio/mpeg" );
contentValues.put ( "_size" , file.length () );
contentValues.put ( "is_ringtone" , Boolean.TRUE );
1

There are 1 best solutions below

0
Ahmed elghetany On

I modified the codes and it worked for me. This is the solution after the modification

 public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (Build.VERSION.SDK_INT >= 31) {
                    if (!Environment.isExternalStorageManager()) {
                        Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                        intent.setData(Uri.parse("package:" + getPackageName()));
                        startActivity(intent);
                    }
                }
            }
        }
    }

    private void requestPermission() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,android. Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            Uri uri = Uri.fromParts("package", getPackageName(), null);
            intent.setData(uri);
            startActivity(intent);
        } else {
            if (Build.VERSION.SDK_INT >= 31) {
                try {
                    Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                    intent.setData(Uri.parse("package:" + getPackageName()));
                    startActivity(intent);
                } catch (ActivityNotFoundException e) {
                    Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
                    startActivity(intent);
                }
            } else {
                ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE,android. Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE);
            }
        }
    }

    private void set_tone(String N1, String N2) {
        AssetFileDescriptor openAssetFileDescriptor;
        ((AudioManager) getSystemService(AUDIO_SERVICE)).setRingerMode(AudioManager.MODE_RINGTONE);
        File file = new File(Environment.getExternalStorageDirectory() + "/AndRody", N2);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Uri parse = Uri.parse("android.resource://" + getPackageName() + "/raw/" + N2);
        ContentResolver contentResolver = getContentResolver();
        try (InputStream inputStream = contentResolver.openInputStream (parse );
             OutputStream outputStream = new FileOutputStream(file)) {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.Audio.Media.DATA, file.getAbsolutePath());
        contentValues.put(MediaStore.Audio.Media.TITLE, N1);
        contentValues.put(MediaStore.Audio.Media.MIME_TYPE, "audio/mpeg");
        contentValues.put(MediaStore.Audio.Media.SIZE, file.length());
        contentValues.put(MediaStore.Audio.Media.IS_RINGTONE, true);

        try {
            Toast.makeText(this, "تم تعيين النغمة بنجاح", Toast.LENGTH_LONG).show();
            RingtoneManager.setActualDefaultRingtoneUri(getBaseContext(), 1, contentResolver.insert(MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath()), contentValues));
        } catch (Throwable th) {
            Toast.makeText(this, "يوجد خلل ما", Toast.LENGTH_LONG).show();
        }
    }