TikTok for Developers

开发者文档

上次更新时间:2026年11月30日

Android FileProvider

入门

步骤 1:集成 FileProvider SDK

// Add the following to gradle:
repositories {
    maven { 
        url 'https://dl.bintray.com/aweme-open-sdk-team/public'
    }
} 
    
dependencies {
    implementation 'com.bytedance.ies.ugc.aweme:opensdk-oversea-external:0.1.4.1'
}

步骤 2:集成

  1. 在Application onCreate中,初始化TikTokOpenApiFactory
@Overridepublic void onCreate() {
 super.onCreate();
    String clientkey = "[Client Key]";
    TikTokOpenApiFactory.init(new TikTokOpenConfig(clientkey));
}
  1. 将权限添加到您的 Android 清单,并设置回调到您的活动
<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" />
// If you have a custom activity to receive callback, you can skip this step
<activity     android:name=".tiktokapi.TikTokEntryActivity"               
  android:launchMode="singleTask"
  android:taskAffinity="[Your package name]" 
  android:exported="true">
</activity>

TikTok 12.5.0 及以上支持 FileProvider。以下是支持 FileProvider 的说明。

FileProvider 用法

  1. 设置

在您的 应用 的 AndroidManifest.xml 中,添加以下设置:

<provider  android:name="android.support.v4.content.FileProvider"
  android:authorities="${applicationPackageName}.fileprovider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
    android:name="android.support.FILE_PROVIDER_PATHS"
    android:resource="@xml/file_provider_paths" />
</provider>

在 res/xml 中(如果不存在请创建该目录),添加文件 file_provider_paths.xml ,其内容为:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="sharedata" path="shareData/"/>
</paths>

external-files-path 代表 应用 外部存储区域根目录中的文件。该子目录的根路径与Context.getExternalFilesDir(null)返回的值相同

您还可以支持其他路径,例如:

<files-path name="name" path="path" /> // Subdirectory returned by Context.getFilesDir()
<cache-path name="name" path="path" /> // Subdirectory returned by Contextget.CacheDir()

欲了解更多信息,请查看此 页.

  1. 使用 FileProvider API

FileProvider 为文件生成 content:// URI。

public String getFileUri(Context context) {
     String filePath = context.getExternalFilesDir(null).p + "/shareData/test.png";
    // The filePath needs conforms to the path in the xml/file_provider_path file in order to be sharable
    File file = new File(filePath);
    // Use contentPath for sharing
    String contentPath = getFileUri(context, file);
 // The package name here must be consistent with what was provided in the AndroidManifest.xml's authorities.
    Uri contentUri = FileProvider.getUriForFile(context, 
        "com.example.app.fileprovider",file);
    // Authorize permission for TikTok build variants
    context.grantUriPermission("com.zhiliaoapp.musically",  
        contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
    context.grantUriPermission("com.ss.android.ugc.trill",  
        contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
    return contentUri.toString(); // contentUri.toString() should be prefixed with "content://"
}
  1. 分享照片
TiktokOpenApi tiktokOpenApi = TikTokOpenApiFactory.create(this);
if (tiktokOpenApi.isShareSupportFileProvider() &&
        android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
    ArrayList<String> images = new ArrayList<>();
    images.add(xxx);  // Pass FileProvider formatted list here
    Share.Request request = new Share.Request();
    ImageObject imageObject = new ImageObject();
    imageObject.mImagePaths = images;
    MediaContent mediaContent = new MediaContent();
    mediaContent.mMediaObject = imageObject;
    request.mMediaContent = mediaContent;
    tiktokOpenApi.share(request);
} else {
    Toast.makeText(TestFileProviderActivity.this, "Unsupported version", Toast.LENGTH_LONG).show();
}
  1. 分享视频
if (tiktokOpenApi.isShareSupportFileProvider() &&
        android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
 
    ArrayList<String> videos = new ArrayList<>();
    videos.add("video path"); // Pass FileProvider formatted list here
    Share.Request request = new Share.Request();
    VideoObject videoObject = new VideoObject();
    videoObject.mVideoPaths = videos;
    MediaContent content = new MediaContent();
    content.mMediaObject = videoObject;
    request.mMediaContent = content;
    tiktokOpenApi.share(request);
} else {
    Toast.makeText(TestFileProviderActivity.this, "Unsupported version", Toast.LENGTH_LONG).show();
}

TikTok for Developers