简体   繁体   English

无法将图像发送到其他应用,尝试远足,Messenger和Whatsapp

[英]Unable to send images to other apps, tried hike, Messenger and Whatsapp

I am trying to share images from my app to other app (Hike, Facebook, Messenger etc) but every time I am getting different error. 我试图将图像从我的应用程序共享到其他应用程序(远足,Facebook,Messenger等),但是每次遇到不同的错误时,我都会尝试共享。 Gone through almost every Q&A but problem not solved yet. 几乎完成了所有问答,但问题尚未解决。

This is my code : 这是我的代码

                       filepath = Environment.getExternalStorageDirectory();
                       cacheDir = new File(filepath.getAbsolutePath()
                       + "/LikeIT/");
                       cacheDir.mkdirs();
                       Intent intent = new Intent();
                       intent.setType("image/jpeg");
                       intent.setAction(Intent.ACTION_SEND);

                       intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(cacheDir
                                       .getAbsolutePath())));
                       intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

                       activity.startActivity(intent);

I have changed the below line many times, but didn't get the solution: 我已经更改了以下几行,但是没有找到解决方案:

    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(cacheDir
                                       .getAbsolutePath())));       

I have changed this with : 我已经用

  1.  intent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse(str1));
  2.  intent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.fromFile(file1);

but didn't get as needed. 但没有得到所需的。 And also when i am sending image to Whatsapp it is not showing image, and after sending it is showing. 而且,当我将图像发送到Whatsapp时,它没有显示图像,并且在发送图像后也显示了图像。

在此处输入图片说明

Try below code that works fine in my app 请尝试以下在我的应用中正常运行的代码

public void onShareItem(View v) {
        // Get access to bitmap image from view

        // Get access to the URI for the bitmap
        Uri bmpUri = getLocalBitmapUri(descpic);
        if (bmpUri != null) {
            // Construct a ShareIntent with link to image
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
            shareIntent.putExtra(Intent.EXTRA_TEXT, desc.getText().toString());
            shareIntent.setType("image/*");
            // Launch sharing dialog for image
            startActivity(Intent.createChooser(shareIntent, "Share Image"));
        } else {
            // ...sharing failed, handle error
            Log.e("check for intent", "Couldn't get anything");
        }
    }

    // Returns the URI path to the Bitmap displayed in specified ImageView
    public Uri getLocalBitmapUri(ImageView imageView) {
        imageView.buildDrawingCache();
        Bitmap bm = imageView.getDrawingCache();

        OutputStream fOut = null;
        Uri outputFileUri=null;
        try {
            File root = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "folder_name" + File.separator);
            root.mkdirs();
            File imageFile = new File(root, "myPicName.jpg");
            outputFileUri = Uri.fromFile(imageFile);
            fOut = new FileOutputStream(imageFile);
        } catch (Exception e) {
            Toast.makeText(this, "Error occured. Please try again later.", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }

        try {
            bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.flush();
            fOut.close();
            return outputFileUri;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

TL;DR ::: You need to enable read/write permissions for WhatsAPP (or any other APP) to use the images. TL; DR :::您需要为WhatsAPP(或任何其他APP)启用读/写权限才能使用图像。 Create a temporary file --- OR --- make your APP save files to external storage. 创建一个临时文件---或---使您的APP将文件保存到外部存储。

This is exactly what happened to me and my app. 这正是我和我的应用程序发生的事情。 Took me a day of fiddling around, and I've finally resolved it. 花了我一天的时间摆弄,我终于解决了。

No matter what, this line of code did not work (or anything relating to modifying the Uri object ) : intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 无论如何,这行代码不起作用(或与修改Uri对象有关的任何事情): intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

Nor the following from FileOutputStream fos = context.openFileOutput("desiredFilename.png", Context.MODE_PRIVATE); FileOutputStream fos = context.openFileOutput("desiredFilename.png", Context.MODE_PRIVATE);的以下内容也不是FileOutputStream fos = context.openFileOutput("desiredFilename.png", Context.MODE_PRIVATE); then changing Context.MODE_PRIVATE to Context.MODE_WORLD_READABLE ... which was deprecated after API-14 然后将Context.MODE_PRIVATE更改为Context.MODE_WORLD_READABLE ...,在API-14之后已弃用

It's not anything wrong with this part: 这部分没有什么问题:

 filepath = Environment.getExternalStorageDirectory();
                       cacheDir = new File(filepath.getAbsolutePath()
                       + "/LikeIT/");
                       cacheDir.mkdirs();
                       Intent intent = new Intent();
                       intent.setType("image/jpeg");
                       intent.setAction(Intent.ACTION_SEND);

                       intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(cacheDir
                                       .getAbsolutePath())));

That part is working just fine, since it starts the WhatsApp process --- and waits for you to add a caption. 该部分工作正常,因为它启动了WhatsApp流程---等待您添加字幕。 By adding intent.putExtra(Intent.EXTRA_TEXT, text_string); 通过添加intent.putExtra(Intent.EXTRA_TEXT, text_string); it'll take whatever string values found in text_string and adds it as the pictures caption. 它会采用在text_string找到的任何字符串值,并将其添加为图片标题。 Unfortunately, intent.putExtra() is received/understood by WhatsAPP with just one picture with one caption ... I'm not sure how to make it multiple pictures with multiple captions... 不幸的是, intent.putExtra()仅通过一张带有一个标题的图片来接收/理解了intent.putExtra() 。我不确定如何使它变成多张带有多个标题的图片...

Another tip: In case the error output says File(s) Directory doesn't exist , it's likely due to the fact that the line mkdirs() did not work. 另一个提示:万一错误输出显示File(s) Directory doesn't exist ,则可能是由于mkdirs()mkdirs() The tutorial uses two chosen directories APP_PATH_SD_CARD and APP_THUMBNAIL_PATH_SD_CARD --- and asks mkdirs() to create both of them at the same time... which for some reason, mkdirs() does not like to do. 本教程使用两个选定的目录APP_PATH_SD_CARDAPP_THUMBNAIL_PATH_SD_CARD ---并要求mkdirs()同时创建两个目录...由于某种原因, mkdirs()不喜欢这样做。 So I asked mkdirs() to create them one at a time: 所以我要求mkdirs()创建一个:

   File dir = new File( Environment.getExternalStorageDirectory().getAbsolutePath() + APP_PATH_SD_CARD );
   if (!dir.exists()) { dir.mkdirs();}

  File dirHoldingImages = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + APP_PATH_SD_CARD + APP_THUMBNAIL_PATH_SD_CARD);
    if (!dirHoldingImages.exists()) { dirHoldingImages.mkdirs(); }

// carry-on with the rest of the saveFileToExternalStorage code

Hope those links helps others that stumble across the same issues. 希望这些链接对遇到相同问题的其他人有所帮助。 :) :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试通过 whatsapp 或任何其他信使应用程序发送/共享 mp3 原始文件。 - I'm trying to send/share mp3 raw file via whatsapp or any other messenger app. 如何从我的应用程序发送应用程序邀请(通过 whatsapp、远足、消息) - How to send app invites from my application (via whatsapp,hike,message) 无法以编程方式发送WhatsApp消息 - Unable to send a WhatsApp message programatically 将 android 通知分享到 whatsapp/电子邮件/其他 3rd 方应用程序 - Share an android notification to whatsapp/email/other 3rd party apps 共享图像到其他应用程序的通用方法 - Universal way to share images to other apps Android与其他应用共享多张图片 - Android Share Multiple Images with Other Apps 从我的应用程序中调用(启动)其他应用程序(如whatsapp)所需的权限是什么 - What are permissions required to call(launch) other apps like whatsapp in my case from our app 正确将可绘制图像共享到其他应用程序(没有FileProvider的情况下,将PNG共享到Whatsapp失败) - Share drawable image to other apps properly (sharing PNG to Whatsapp fails without FileProvider) 无法从我的 WebView 在 Whatsapp 上分享。 无法加载 whatsapp://send?text= 处的网页,因为 net::ERR_UNKNOWN_URL_SCHEME - Unable to share on Whatsapp from my WebView . Getting The webpage at whatsapp://send?text= could not be load because net::ERR_UNKNOWN_URL_SCHEME 自动发送whatsapp消息 - To send whatsapp message automatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM