gpt4 book ai didi

java - 通过 Content Provider 的 Android 铃声

转载 作者:行者123 更新时间:2023-12-01 13:07:28 27 4
gpt4 key购买 nike

我想将我的资源文件夹中的铃声设置为默认铃声。现在我发现这是不可能的,因为无法从外部访问 Assets 。因此,我必须将文件从我的 Assets 复制到 SD,或者我使用内容提供程序。我认为最后一个是更好的方法。

但是我没能成功,希望你能帮助我。这是我的内容提供商:

public class ringtoneContentProvider extends ContentProvider {
private static final String TAG = "ringtoneCP";
private static String[] mimeTypes = {"audio/ogg"};
private Uri generatedUri;

@Override
public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
Log.d("provider", "Provider openAssetFile wird aufgerufen");
AssetManager am = getContext().getAssets();
String file_name = uri.getLastPathSegment();
if(file_name == null)
throw new FileNotFoundException();
AssetFileDescriptor afd = null;
try {
afd = am.openFd(file_name);
} catch (IOException e) {
e.printStackTrace();
}
return afd;//super.openAssetFile(uri, mode);
}

@Override
public String getType( Uri p1 )
{
Log.d("provider","provider getType");
// TODO: Implement this method
return "audio/ogg";
}

@Override
public int delete( Uri p1, String p2, String[] p3 )
{
// TODO: Implement this method
throw new RuntimeException("ContentProvider.delete not supported");
}

@Override
public Cursor query( Uri p1, String[] p2, String p3, String[] p4, String p5 )
{
Log.d("provider","provider query 1");
// TODO: Implement this method
return null;
}

@Override
public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal )
{
Log.d("provider","provider query 2");
// TODO: Implement this method
return super.query( uri, projection, selection, selectionArgs, sortOrder, cancellationSignal );
}

@Override
public Uri insert( Uri p1, ContentValues p2 )
{
Log.d("provider","provider insert");
// TODO: Implement this method
return null;
}

@Override
public boolean onCreate( )
{
Log.d("provider","provider onCreate");
generatedUri = Uri.EMPTY;
return true;
}

@Override
public String[] getStreamTypes(Uri uri, String mimeTypeFilter) {
Log.d("provider","provider getStreamTypes");
return mimeTypes;
}

@Override
public int update( Uri p1, ContentValues p2, String p3, String[] p4 )
{
Log.d("provider","provider update");
// TODO: Implement this method
return 0;
}
}

这里我想设置铃声:

RingtoneManager.setActualDefaultRingtoneUri(
getApplicationContext(),
RingtoneManager.TYPE_RINGTONE,
Uri.parse("content://com.xyz.ringtoneContentProvider/ringtone.ogg")
);

但这行不通。所以我这样做:

Uri myFile = Uri.parse("content://com.xyz.ringtoneContentProvider/ringtone.ogg");

//We now create a new content values object to store all the information
//about the ringtone.
ContentValues values = new ContentValues();
//values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
values.put(MediaStore.MediaColumns.DATA, myFile.getPath());
values.put(MediaStore.MediaColumns.TITLE, "MyRingtone"); // file.getName());
//values.put(MediaStore.MediaColumns.SIZE, file.length());
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
values.put(MediaStore.Audio.AudioColumns.ARTIST, getApplicationContext().getString(R.string.app_name));
values.put(MediaStore.Audio.AudioColumns.IS_RINGTONE, true);
values.put(MediaStore.Audio.AudioColumns.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.AudioColumns.IS_ALARM, false);
values.put(MediaStore.Audio.AudioColumns.IS_MUSIC, false);

//Work with the content resolver now
//First get the file we may have added previously and delete it,
//otherwise we will fill up the ringtone manager with a bunch of copies over time.
Uri uri = MediaStore.Audio.Media.getContentUriForPath(myFile.toString());
getApplicationContext().getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + myFile + "\"", null);


//Ok now insert it
Uri newUri = getApplicationContext().getContentResolver().insert(myFile, values);

//Ok now set the ringtone from the content manager's uri, NOT the file's uri
RingtoneManager.setActualDefaultRingtoneUri(
getApplicationContext(),
RingtoneManager.TYPE_RINGTONE,
newUri
);

但这也不起作用。

<小时/>

------------- 使用流提供者 -------------

对于 Steam 提供商,如果我打开“设置应用程序/声音”,则会收到以下错误:

The exact Error Message is: Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading com.commonsware.cwac.provider.StreamProvider uri content://com.xyz.myStreamProvider/ringtone.ogg from pid=1446, uid=1000 requires the provider be exported, or grantUriPermission()
at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:467)
at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:394)
at android.content.ContentProvider$Transport.query(ContentProvider.java:194)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
at android.os.Binder.execTransact(Binder.java:404)
at dalvik.system.NativeStart.run(Native Method)

如果我执行 android:exported="true"和 android:grantUriPermissions="true"我会收到此错误消息:

FATAL EXCEPTION: main
Process: com.xyz.app, PID: 1515
java.lang.RuntimeException: Unable to get provider com.commonsware.cwac.provider.StreamProvider: java.lang.SecurityException: Provider must not be exported
at android.app.ActivityThread.installProvider(ActivityThread.java:4793)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4385)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4325)
at android.app.ActivityThread.access$1500(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.SecurityException: Provider must not be exported
at com.commonsware.cwac.provider.StreamProvider.attachInfo(StreamProvider.java:65)
at android.app.ActivityThread.installProvider(ActivityThread.java:4790)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4385)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4325)
at android.app.ActivityThread.access$1500(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)

我能做什么?

最佳答案

你想要的东西用 StreamProvider 是不可能实现的(或 Android 的 FileProvider ),因为两者都仅限于由 Activity 发起流请求的情况,您可以使用 FLAG_GRANT_URI_READ_PERMISSION允许选择性访问内容。欢迎您fork StreamProvider解除此限制。

但是,您将遇到这样的问题:媒体播放器往往需要可搜索的流,而通过 StreamProvider 提供的资源是不可能实现这一点的。 .

您的选择是:

  1. 将文件复制到外部存储。

  2. 将文件复制到内部存储,并使用 FileProvider 的 fork 版本或StreamProvider这解除了不得导出的限制,因为由简单文件支持的流在 FileProvider 提供服务时是可查找的或StreamProvider .

关于java - 通过 Content Provider 的 Android 铃声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23162826/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com