android - 单击我的按钮时如何共享我的屏幕转换?

标签 android android-intent android-file screensharing

我写了这段代码:当点击我的分享按钮时,是的,我的屏幕照片保存到 SD 卡但是当我在 facebook 或蓝牙上按分享时它说找不到。如果我手动选择照片我可以分享但我想当我单击共享按钮时共享我的屏幕照片?我该怎么做?

mylayout.setDrawingCacheEnabled(true);




// this is the important code :)
// Without it the view will have a
// dimension of 0,0 and the bitmap will
// be null




mylayout.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
//v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
mylayout.layout(0, 0, mylayout.getWidth(), mylayout.getHeight());
mylayout.buildDrawingCache(true);
Bitmap bm = Bitmap.createBitmap(mylayout.getDrawingCache());
mylayout.setDrawingCacheEnabled(false); //




if (bm != null) {
    try {
        String  path2 = Environment.getExternalStorageDirectory().toString();
        OutputStream fOut = null;
        File file = new File(path2, "screentest.jpg");
        pathim=file.getPath();
        fOut = new FileOutputStream(file);




        bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
        fOut.flush();
        fOut.close();




        Log.e("ImagePath", "Image Path : "
                + MediaStore.Images.Media.insertImage(
                        getContentResolver(), file.getAbsolutePath(),
                        file.getName(), file.getName()));
    } catch (Exception e) {
        e.printStackTrace();
    }

}



Intent sharingIntent = new Intent(Intent.ACTION_SEND); 
Uri screenshotUri = Uri.parse(pathim);


sharingIntent.setType("image/jpg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "share with"));

最佳答案

尝试改变这一行:

Uri screenshotUri = Uri.parse(pathim);

进入:

Uri screenshotUri = Uri.fromFile(file);

并将 File file 声明移到 if() 之外

关于android - 单击我的按钮时如何共享我的屏幕转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9099407/

相关文章:

android - Android 中使用 .map 文件的离线 map

java - 使用 if 语句比较字符串

java - Android Studio 中的 Aviary 集成

java - Activity 权限拒绝

android - 检查 Intent uri 是否可用

android - 在android中获取预加载文件

android - 从 URI 转换为 Uri,反之亦然

android - 如果没有 @Provides 方法,则无法提供 Dagger 2 Named

android - 通过 url intent 过滤器启动 Android 设置应用程序

java - Android - 复制文件后关闭电源导致文件损坏