
1. MODE_WORLD_READABLE 模式廢棄
Caused by: java.lang.SecurityException: MODE_WORLD_READABLE no longer supported
MODE_WORLD_READABLE 模式換成 MODE_PRIVATE
2. 獲取通話記錄
Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.CallLogProvider from ProcessRecord{8c75d80 31624:com.ct.client/u0a122} (pid=31624, uid=10122) requires android.permission.READ_CALL_LOG or android.permission.WRITE_CALL_LOG
針對(duì)android.permission.READ_CALL_LOG or android.permission.WRITE_CALL_LOG做權(quán)限適配
3. 圖片選擇和裁剪
Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.ct.client/files/com.ct.client/camere/1547090088847.jpg exposed beyond app through ClipData.Item.getUri()
第一步:
在AndroidManifest.xml清單文件中注冊(cè)provider
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.ct.client.fileProvider"
android:grantUriPermissions="true"
android:exported="false">
<!--元數(shù)據(jù)-->
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
需要注意一下幾點(diǎn):
- exported:必須為false
- grantUriPermissions:true,表示授予 URI 臨時(shí)訪問權(quán)限。
- authorities 組件標(biāo)識(shí),都以包名開頭,避免和其它應(yīng)用發(fā)生沖突。
第二步:
指定共享文件的目錄,需要在res文件夾中新建xml目錄,并且創(chuàng)建file_paths
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<paths>
<external-path path="" name="download"/>
</paths>
</resources>
path=”“,是有特殊意義的,它代表根目錄,也就是說(shuō)你可以向其它的應(yīng)用共享根目錄及其子目錄下任何一個(gè)文件了。
第三步:
使用FileProvider
- 根據(jù)版本號(hào)把Uri改成使用FiliProvider創(chuàng)建的Uri,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
cameraFileUri = FileProvider.getUriForFile(mContext, "com.ct.client.fileProvider", new File(saveCamerePath, saveCameraFileName));
} else {
cameraFileUri = Uri.fromFile(new File(saveCamerePath, saveCameraFileName));
}
- 添加intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)來(lái)對(duì)目標(biāo)應(yīng)用臨時(shí)授權(quán)該Uri所代表的文件
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//添加這一句表示對(duì)目標(biāo)應(yīng)用臨時(shí)授權(quán)該Uri所代表的文件
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
- 在設(shè)置裁剪要保存的
intent.putExtra(MediaStore.EXTRA_OUTPUT, outUri);的時(shí)候,這個(gè)outUri是要使用Uri.fromFile(file)生成的,而不是使用FileProvider.getUriForFile。
FileProvider配置異常
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.ffcs.wisdom/files/com.ffcs.wisdom/camere/1548396305034.jpg
FileProvider所支持的幾種path類型
從Android官方文檔上可以看出FileProvider提供以下幾種path類型:
<files-path path="" name="camera_photos" />
該方式提供在應(yīng)用的內(nèi)部存儲(chǔ)區(qū)的文件/子目錄的文件。它對(duì)應(yīng)Context.getFilesDir返回的路徑:eg:"/data/data/com.jph.simple/files"。
<cache-path name="name" path="path" />
該方式提供在應(yīng)用的內(nèi)部存儲(chǔ)區(qū)的緩存子目錄的文件。它對(duì)應(yīng)getCacheDir返回的路徑:eg:“/data/data/com.jph.simple/cache”;
<external-path name="name" path="path" />
該方式提供在外部存儲(chǔ)區(qū)域根目錄下的文件。它對(duì)應(yīng)Environment.getExternalStorageDirectory返回的路徑:eg:"/storage/emulated/0";
<external-files-path name="name" path="path" />
該方式提供在應(yīng)用的外部存儲(chǔ)區(qū)根目錄的下的文件。它對(duì)應(yīng)Context#getExternalFilesDir(String) Context.getExternalFilesDir(null)返回的路徑。eg:"/storage/emulated/0/Android/data/com.jph.simple/files"。
<external-cache-path name="name" path="path" />
該方式提供在應(yīng)用的外部緩存區(qū)根目錄的文件。它對(duì)應(yīng)Context.getExternalCacheDir()返回的路徑。eg:"/storage/emulated/0/Android/data/com.jph.simple/cache"。
4. 獲取以content開頭的文件拿不到正確路徑
java.lang.IllegalArgumentException: column '_data' does not exist
拿到uri之后進(jìn)行版本判斷大于等于24(即Android7.0)用最新的獲取路徑方式
String str ="";
if (Build.VERSION.SDK_INT >= 24) {
str = getFilePathFromURI(this, uri);//新的方式
} else {
str = getPath(this, uri);你自己之前的獲取方法
}
public String getFilePathFromURI(Context context, Uri contentUri) {
File rootDataDir = context.getFilesDir();
String fileName = getFileName(contentUri);
if (!TextUtils.isEmpty(fileName)) {
File copyFile = new File(rootDataDir + File.separator + fileName);
copyFile(context, contentUri, copyFile);
return copyFile.getAbsolutePath();
}
return null;
}
public static String getFileName(Uri uri) {
if (uri == null) return null;
String fileName = null;
String path = uri.getPath();
int cut = path.lastIndexOf('/');
if (cut != -1) {
fileName = path.substring(cut + 1);
}
return fileName;
}
public void copyFile(Context context, Uri srcUri, File dstFile) {
try {
InputStream inputStream = context.getContentResolver().openInputStream(srcUri);
if (inputStream == null) return;
OutputStream outputStream = new FileOutputStream(dstFile);
copyStream(inputStream, outputStream);
inputStream.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public int copyStream(InputStream input, OutputStream output) throws Exception, IOException {
final int BUFFER_SIZE = 1024 * 2;
byte[] buffer = new byte[BUFFER_SIZE];
BufferedInputStream in = new BufferedInputStream(input, BUFFER_SIZE);
BufferedOutputStream out = new BufferedOutputStream(output, BUFFER_SIZE);
int count = 0, n = 0;
try {
while ((n = in.read(buffer, 0, BUFFER_SIZE)) != -1) {
out.write(buffer, 0, n);
count += n;
}
out.flush();
} finally {
try {
out.close();
} catch (IOException e) {
}
try {
in.close();
} catch (IOException e) {
}
}
return count;
}
5. 7.0的手機(jī)安裝沒問題,但是在8.0上安裝,app沒有反應(yīng),一閃而過(guò)
- 增加新權(quán)限
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
- 把
Intent intent = new Intent(Intent.ACTION_VIEW)
改為
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
6. 解析包安裝失敗。
安裝時(shí)把intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)這句話放在intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)前面。
7. 通知欄不顯示
在Application中創(chuàng)建渠道
@Override
protected void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "chat";
String channelName = "聊天消息";
int importance = NotificationManager.IMPORTANCE_HIGH;
createNotificationChannel(channelId, channelName, importance);
channelId = "subscribe";
channelName = "訂閱消息";
importance = NotificationManager.IMPORTANCE_DEFAULT;
createNotificationChannel(channelId, channelName, importance);
}
}
@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel(String channelId, String channelName, int importance) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}
根據(jù)渠道發(fā)送消息new NotificationCompat.Builder(this, channelName)
public class MainActivity extends AppCompatActivity {
...
public void sendChatMsg(View view) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this, "chat")
.setContentTitle("收到一條聊天消息")
.setContentText("今天中午吃什么?")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))
.setAutoCancel(true)
.build();
manager.notify(1, notification);
}
public void sendSubscribeMsg(View view) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this, "subscribe")
.setContentTitle("收到一條訂閱消息")
.setContentText("地鐵沿線30萬(wàn)商鋪搶購(gòu)中!")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))
.setAutoCancel(true)
.build();
manager.notify(2, notification);
}
}
8. 懸浮窗適配
使用 SYSTEM_ALERT_WINDOW 權(quán)限的應(yīng)用無(wú)法再使用以下窗口類型來(lái)在其他應(yīng)用和系統(tǒng)窗口上方顯示提醒窗口:
- TYPE_PHONE
- TYPE_PRIORITY_PHONE
- TYPE_SYSTEM_ALERT
- TYPE_SYSTEM_OVERLAY
- TYPE_SYSTEM_ERROR
相反,應(yīng)用必須使用名為 TYPE_APPLICATION_OVERLAY 的新窗口類型。
也就是說(shuō)需要在之前的基礎(chǔ)上判斷一下:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mWindowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
}else {
mWindowParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
}
需要新增權(quán)限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
9. 隱式廣播例外
ACTION_LOCKED_BOOT_COMPLETED, ACTION_BOOT_COMPLETED
免除,因?yàn)檫@些廣播僅在首次啟動(dòng)時(shí)發(fā)送一次,并且許多應(yīng)用需要接收此廣播以安排作業(yè),警報(bào)等。
注意:WorkManager是一個(gè)新的API,目前采用alpha格式,允許您安排需要保證完成的后臺(tái)任務(wù)(無(wú)論應(yīng)用程序進(jìn)程是否存在)。WorkManager為API 14+設(shè)備提供類似JobScheduler的功能,即使是那些沒有Google Play服務(wù)的設(shè)備。WorkManager是可查詢的(可觀察的),對(duì)工作圖表和流暢的API有很強(qiáng)的支持。如果您使用的是JobScheduler,F(xiàn)ireBaseJobScheduler和/或AlarmManager以及BroadcastReceivers,則應(yīng)考慮使用WorkManager。
10. BroadcastReceiver無(wú)法接收廣播
intent.setComponent(new ComponentName(mContext, AppWidgetProvider.class)); // 8.0以上版本必須寫