无法将图像发送到其他应用程序,试图加息,Messenger和WhatsApp的 [英] Unable to send images to other apps, tried hike, Messenger and Whatsapp

查看:254
本文介绍了无法将图像发送到其他应用程序,试图加息,Messenger和WhatsApp的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的应用程序与其他应用程序(远足,脸谱,信使等),但每次我得到不同的错误时间共享图像。通过几乎每一个Q&放了; A,但问题没有解决。

I am trying to share images from my app to other app (Hike, Facebook, Messenger etc) but every time I am getting different error. Gone through almost every Q&A but problem not solved yet.

这是我的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);

我已经改变了下面这一行很多次,但没有得到解决:

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

我跟改变了这个

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

但没有得到需要。并且,当我发送图片的WhatsApp它没有显示图像,它显示在发送后。

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.

在这里输入的形象描述

推荐答案

尝试下面code,在我的应用程序正常工作

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;
    }

这篇关于无法将图像发送到其他应用程序,试图加息,Messenger和WhatsApp的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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