有条件地打开基于 Uri 的意向



我试图做到这一点,如果用户单击"共享"按钮,并且 uri 中不存在图像,则不会启动意图(防止 nullexception 崩溃)。

否则,请继续允许共享意图。

case R.id.meetBtn: 
                Uri photoUri = Uri.parse(sImage.toString());
                if (photoUri != null || sImage.toString() != null || sImage.toString() != "")
                {
                    Intent shareIntent = new Intent(Intent.ACTION_SEND);
                    shareIntent.setData(photoUri);
                    shareIntent.setType("image/png");
                    shareIntent.putExtra(Intent.EXTRA_STREAM, photoUri);
                    startActivity(Intent.createChooser(shareIntent, "Use this for sharing"));
                }
                break;
            }

如您所见,我尝试了一些不同的东西,但仍然以崩溃启动。

我建议使用Try-Catch块而不是if语句。

try {
     [insert code here]
    } catch (Exception e) {
     [Log Error or create error toast]
}

我建议使用更具体的错误捕获,但这是基本的必要结构。

有关 try-catch 块的更多详细信息,请访问以下链接:避免在安卓上尝试/赶上

对于官方文档

http://docs.oracle.com/javase/tutorial/essential/exceptions/try.html - 跟随捕获,最后也从那里链接。

最新更新