首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Android上分享屏幕截图。每次共享相同的图像

在Android上分享屏幕截图。每次共享相同的图像
EN

Stack Overflow用户
提问于 2014-05-21 08:18:09
回答 2查看 487关注 0票数 0

我创建了一个函数,将截图保存在.jpeg类型的临时文件中,然后允许用户在facebook或蓝牙上共享。以下是我的分享功能:

代码语言:javascript
复制
public Bitmap Share(View v) {
    // Sound
    soundPool.play(button_sound, 1.0f, 1.0f, 0, 0, 1.0f);

    // Image
    v.setDrawingCacheEnabled(true);
    v.setLayerType(View.LAYER_TYPE_NONE, null);
    Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
    File file = new File(Environment.getExternalStorageDirectory()
            + File.separator + "temporary_file.jpg");
    try {
        file.createNewFile();
        FileOutputStream ostream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, ostream);
        ostream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Share
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/jpeg");
    String filel = "file://" + Environment.getExternalStorageDirectory()
            + File.separator + "temporary_file.jpg";
    share.putExtra(Intent.EXTRA_STREAM, Uri.parse(filel));

    startActivity(Intent.createChooser(share, "Share Image"));
    return bitmap;
}

我的问题是,它需要截图,但当我尝试分享一个新的截图时,总是反复分享相同的截图。当我使用文件管理器检查时,图像是不同的。所以我不知道是什么原因造成的。

非常感谢你抽出时间。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-21 09:40:06

首先,只有当文件不存在时,file.createNewFile()才能工作。它不会在失败时抛出任何异常,它只返回true表示成功,返回false表示失败,

bitmap.compress(Bitmap.CompressFormat.JPEG, 90, ostream);也是如此。此方法也不会在失败时抛出任何异常,只返回true表示成功,返回false表示失败,

我不知道它是否导致了错误,但您可能需要研究一下。例如,当文件已经存在时,您可以尝试先删除它。

票数 0
EN

Stack Overflow用户

发布于 2014-05-21 09:12:39

请在getBitmap之前使您的视图无效

看上去:

代码语言:javascript
复制
v.inValidated(); 
v.setDrawingCacheEnabled(true);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23777687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档