【问题标题】:Android Share Intent Image Sharing not workingAndroid共享意图图像共享不起作用
【发布时间】:2016-08-11 10:43:45
【问题描述】:

我是 Android 新手,在通过共享意图共享图像时遇到问题。我用谷歌搜索了很多,尝试了各种方法,但仍然找不到解决方案。

我的代码是:

            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setType("image/*");
            shareIntent.putExtra(Intent.EXTRA_STREAM,uri);
            this.startActivityForResult(Intent.createChooser(shareIntent,"Share with"),12);

我检查了 uri,保存的文件是位图及其返回文件。但分享时显示的图片无效。 Gmail 说它无法附加附件,消息应用程序无法加载图像。

文本共享工作正常。

基本上我正在为 Unity 编写一个插件。这是我在 Unity 端的代码:

string destination = Path.Combine(Application.persistentDataPath,"sharingImage" + ".png");
            if (!File.Exists(destination)) {
                print("creating new file");
                File.Create(destination);
            }
            File.WriteAllBytes(destination, bytes);

            print("destination= "+destination);

            AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
            AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse","file://" + destination);

            using (AndroidJavaClass pluginClass = new AndroidJavaClass("com.kashiftasneem.sharelib.ShareController")) {

                AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");

                pluginClass.CallStatic("Share",message,uriObject,jo,gameObject.name,destination);
            }

我正在记录目的地和 uri,它们是:

目的地= /data/data/com.kashiftasneem.shareandroidplugin2/files/sharingImage.png

uri = file:///data/data/com.kashiftasneem.shareandroidplugin2/files/sharingImage.png

【问题讨论】:

    标签: android android-intent android-activity uri share-intent


    【解决方案1】:
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
    shareIntent.setType("image/jpeg");
    startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
    

    这可能会有所帮助。

    【讨论】:

    • 我认为这可能是 uri 的问题。
    • 我已编辑问题以包含更多与 uri 相关的信息。
    【解决方案2】:

    试试这个代码:

            Bitmap bitmap = getBitmapFromView(idForSaveView);
            try {
                File file = new File(this.getExternalCacheDir(), "appdiam.png");
                FileOutputStream fOut = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                fOut.flush();
                //fOut.close();
                file.setReadable(true, false);
                final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                intent.setType("image/png");
                startActivity(Intent.createChooser(intent, "Share image via"));
            } catch (Exception e) {
                e.printStackTrace();
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-03
      • 2022-06-25
      • 1970-01-01
      • 2015-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多