Android在Snapchat上分享图片 [英] Android share image on snapchat

查看:473
本文介绍了Android在Snapchat上分享图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码共享乐谱的屏幕截图:

I'm using this code to share a screenshot of the score:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("image/png");
        Uri image = Uri.parse(Environment.getExternalStorageDirectory().toString() + "/sharescore.png");
        try {
            // create bitmap screen capture
            View v1 = v.getRootView();
            v1.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
            v1.setDrawingCacheEnabled(false);

            File imageFile = new File(image.getPath());

            FileOutputStream outputStream = new FileOutputStream(imageFile);
            int quality = 100;
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (Throwable e) {
            // Several error may come out with file handling or OOM
            e.printStackTrace();
        }
        sharingIntent.putExtra(Intent.EXTRA_STREAM, image);
        startActivity(Intent.createChooser(sharingIntent, getString(R.string.share_via)));

当我使用"imgae/jpeg"类型时,它对于Whatsapp Twitter等也很好,但对于手套和Instagram而言则不然.它导致错误无法打开所选图像".我该如何在手套和instagram上使用这项功能?

It works fine for Whatsapp Twitter etc but not for snapchat and Instagram also when I use the "imgae/jpeg" type. It results in the error "selected image cant be opened". How can I make this work for snapchat and instagram.

推荐答案

他们的问题是,我将屏幕截图保存到了sd卡中,但是gmail没有权限从外部存储中读取文件.我必须使用只能由我的应用程序访问的应用程序目录,并且必须使用我自己的fileprovider(com.test.fileprovider)更改权限,以便使手套可以访问它.这是代码:

They Problem was, that I saved the screenshot to the sd-card, but snapchat doesnt have the permission to read files from the external storage. I had to use the app Directory that can only be accesed by my app and had to change the permissions with my own fileprovider (com.test.fileprovider) so that snapchat can acess it. Here is the Code:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("image/jpg");
        sharingIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        File newFile = new File(getContext().getCacheDir(), "share_score.jpg");
        Log.d(getClass().getSimpleName(), newFile.getAbsolutePath());
        Uri image = FileProvider.getUriForFile(getContext(), "com.test.fileprovider", newFile);
        try {
            // create bitmap screen capture
            View v1 = v.getRootView();
            v1.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
            v1.setDrawingCacheEnabled(false);

            FileOutputStream outputStream = new FileOutputStream(newFile);
            int quality = 100;
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (Throwable e) {
            // Several error may come out with file handling or OOM
            e.printStackTrace();
        }
        sharingIntent.putExtra(Intent.EXTRA_STREAM, image);
        startActivity(Intent.createChooser(sharingIntent, getString(R.string.share_via)));

这篇关于Android在Snapchat上分享图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆