在Nougat中,此功能无效.
String path = getRealPathFromURI(this, getIntent().getParcelableExtra(Intent.EXTRA_STREAM));
Run Code Online (Sandbox Code Playgroud)
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
if (cursor == null) return contentUri.getPath();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
崩溃日志:
java.lang.RuntimeException: Unable to start activity ComponentInfo{class path}: java.lang.IllegalArgumentException: column '_data' does not exist
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at …Run Code Online (Sandbox Code Playgroud) 有时我的应用程序会遇到这种异常:
Fatal Exception: android.app.RemoteServiceException: Bad notification posted from package com.packagename: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.packagename user=UserHandle{0} id=136094146 tag=null score=0: Notification(pri=0 contentView=com.packagename/0x109007e vibrate=default sound=default defaults=0xffffffff flags=0x11 kind=[null]))
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(NativeStart.java)
Run Code Online (Sandbox Code Playgroud)
代码创建通知:
PendingIntent intent = PendingIntent.getActivities(this, id,
notificationIntents, PendingIntent.FLAG_UPDATE_CURRENT);
int color = ContextCompat.getColor(this, R.color.notif_background);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentText(description)
.setSmallIcon(getSmallIcon())
.setLargeIcon(getLargeIcon())
.setColor(color)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigPictureStyle().bigLargeIcon(largeIcon))
.setContentIntent(intent);
if (title != null) { …Run Code Online (Sandbox Code Playgroud) 我引用了示例应用程序代码(https://github.com/googlesamples/android-DirectShare),然后将ChooserTargetService添加到了我的应用程序中
public class ShareChooserTargetService extends ChooserTargetService {
@Override
public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName,
IntentFilter matchedFilter) {
Log.d("ShareChooserTargetService", "onGetChooserTargets: ");
ComponentName componentName = new ComponentName(getPackageName(),
ShareActivity.class.getCanonicalName());
ArrayList<ChooserTarget> targets = new ArrayList<>();
for (User user : Users.getAll()) {
Bundle extras = new Bundle();
extras.putInt("user_id", user.id);
targets.add(new ChooserTarget(
user.name,
Icon.createWithResource(this, R.mipmap.ic_user),
componentName,
extras));
}
return targets;
}
Run Code Online (Sandbox Code Playgroud)
在清单文件中,我添加了以下几行:
<activity
android:name=".ui.ShareActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
<meta-data
android:name="android.service.chooser.chooser_target_service"
android:value=".ShareChooserTargetService" />
</activity>
<service
android:name=".ShareChooserTargetService" …Run Code Online (Sandbox Code Playgroud) 我找不到合适的解决方案.下面的代码给了我本地IP地址(如果我连接到Wifi,它提供的IP地址如192.168.0.x),但我想要公共IP地址(就像我在谷歌搜索"我的IP是什么")
public static String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
要么
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?谢谢!