Android 当作为额外目的传递时,图像不显示

Android 当作为额外目的传递时,图像不显示,android,android-intent,Android,Android Intent,我试图通过一个明确的意图屏幕截图,但屏幕显示黑色截图(参考图片)。我一单击共享,就会出现一个祝酒词,说发送失败。以下是捕获屏幕截图并将其发送到其他应用程序的代码: public void getScreenShot(View view) { View screenView = view.getRootView(); screenView.setDrawingCacheEnabled(true); Bitmap bitmap = Bitmap.cre

我试图通过一个明确的意图屏幕截图,但屏幕显示黑色截图(参考图片)。我一单击共享,就会出现一个祝酒词,说发送失败。以下是捕获屏幕截图并将其发送到其他应用程序的代码:

public void getScreenShot(View view) {
        View screenView = view.getRootView();
        screenView.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
        screenView.setDrawingCacheEnabled(false);
        f = new File(this.getFilesDir(), "screenshotFile");
        try {
            if (!f.exists())
                f.createNewFile();
        } catch (IOexception e) {
            e.printStackTrace();
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 10, bos);
        byte[] bitmapdata = bos.toByteArray();

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(f);
            fos.write(bitmapdata);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
此代码将数据发送到whatsapp:

public void shareWhatsapp(View view) {
    try {
        myVib.vibrate(50);
        getScreenShot(view);

        //String fileName = "screenshotFile";
        //Bitmap bitmap = BitmapFactory.decodeFile(f.getAbsolutePath());

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/*");
        try {
            intent.setPackage("com.whatsapp");
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(MainActivity.this, "App not installed", Toast.LENGTH_SHORT).show();
        }
        //TODO: APP CAN CRASH HERE
        if (position > 0) {
            try {
                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                intent.putExtra(Intent.EXTRA_TEXT, Titles.get(position - 1) + ": " + Links.get(position - 1));       //position problems
            }
        } else {
            try {
                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                intent.putExtra(Intent.EXTRA_TEXT, Titles.get(0) + ": " + Links.get(0));       //position problems
            }
        }
        startActivity(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

有人能帮我吗?

首先,您要将文件写入。第三方应用无法访问你的应用的内部存储

其次,您使用的是
Uri.fromFile()
,即


你最安全的长期做法是,然后使用与
ContentProvider

相关联的
Uri
,在哪个版本的Android上进行测试?Android 6.0棉花糖也许downvoter可以帮上忙,因为他可能有太多的空闲时间不阅读整个问题。尝试使用保存到外部存储,但仍然存在保存问题。无法访问该文件。清单中提到的所有权限。java.io.filenotfoundexception打开失败的eacces(权限被拒绝)