首页 > 解决方案 > 为什么 sharesheet 意图使我的 android 应用程序崩溃?

问题描述

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_class);
        btn = (Button) findViewById(R.id.button2);
        imview = findViewById(R.id.imageView);
        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        final File imageFile = new File(path, "enc" + ".png");
        Bitmap bmp = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
        imview.setImageBitmap(bmp);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri urii =  Uri.fromFile(imageFile);
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("image/*");
                intent.putExtra(Intent.EXTRA_STREAM, urii);
                startActivity(Intent.createChooser(intent , "Share"));
            }
        });

上面的代码在我的模拟器上运行良好,实际上速度正常。但是,当我安装 apk 时,此活动会使屏幕空白几秒钟。恢复后,图像视图工作正常,但当我单击按钮时,应用程序崩溃。我不知道如何调试它。

注意:这是在数据写入后加载的布局。但我怀疑数据是否写入缓慢。所以我在按钮功能中包含了文件读取语句。但是应用程序仍然崩溃。

标签: androidandroid-studio

解决方案


在图像完全加载之前使用coroutine或放置progressbar


推荐阅读