使用Intent在什么应用上共享图像遇到错误共享失败 [英] Sharing Image Using Intent on whats app getting error sharing failed

查看:74
本文介绍了使用Intent在什么应用上共享图像遇到错误共享失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在清单中的可绘制文件夹集文件提供者权限中加载单个jpg图像,但是当我在whatsapp上共享图像时,我遇到了错误共享失败的情况,请重试.当我共享文本时,它可以正常工作,但是当我尝试共享图像时,它给出了我出错.以下是文件

I am loading single jpg image from drawable folder set file provider permission in manifest but when i share image on whatsapp i got error sharing failed please try again.When i share text it works fine but when i tried to share image it gives me error.Here is following files

Fullscreenadapter.java

package com.mobdev.birthdaycakesquotes;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.app.ShareCompat;
import android.support.v4.content.FileProvider;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;



public class Fullscreenadapter extends PagerAdapter {
private Integer[] Image;
private int _resource;
private Activity _activity;
private LayoutInflater inflater;
private  String images;
TextView t1,t2;
public Fullscreenadapter(Activity activity,
                         Integer[] image) {
    this._activity = activity;
    this.Image=image;
}

@Override
public int getCount() {
    return this.Image.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
    final ImageView imgDisplay,shareimage;


    inflater = (LayoutInflater) _activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View viewLayout = inflater.inflate(R.layout.fullscreenlayoutdesign, 
  container,
            false);

    imgDisplay = (ImageView) viewLayout.findViewById(R.id.show);
    shareimage=(ImageView)viewLayout.findViewById(R.id.share);
    t1=(TextView)viewLayout.findViewById(R.id.currentposition);
    t2=(TextView)viewLayout.findViewById(R.id.totalimage);


    imgDisplay.setImageResource(Image[position]);
    t1.setText(String.valueOf(position));
    t2.setText(String.valueOf(Image.length));
    images=createImageOnSDCard(R.drawable.sw);

    shareimage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

     Uri path = FileProvider.getUriForFile(_activity, 
    "com.mobdev.birthdaycakesquotes",new File(images));


            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_TEXT, "This is one image I'm 
   sharing.");
            shareIntent.putExtra(Intent.EXTRA_STREAM, path);
            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            shareIntent.setType("image/*");
            _activity.startActivity(Intent.createChooser(shareIntent, 
  "Share..."));


        }
    });
    ((ViewPager) container).addView(viewLayout);
    return viewLayout;
}
private String createImageOnSDCard(int resID) {
    Bitmap bitmap = BitmapFactory.decodeResource(Resources.getSystem(), resID);
    String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + resID + ".jpg";
    File file = new File(path);
    try {
        OutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return file.getPath();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewPager) container).removeView((RelativeLayout) object);
}
}

AndroidManifestfile

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mobdev.birthdaycakesquotes">

<application
    android:allowBackup="true"
    android:hardwareAccelerated="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".HomePage"
        android:label="@string/title_activity_home_page"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
    <activity android:name=".Details_activity"></activity>

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.mobdev.birthdaycakesquotes"
        android:grantUriPermissions="true"
        android:exported="false">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>
</application>

file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="." />
</paths>

推荐答案

private void shareImage(Uri imagePath) {
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    sharingIntent.setType("image/*");
    sharingIntent.putExtra(Intent.EXTRA_STREAM, imagePath);
    //sharingIntent.setPackage("com.whatsapp"); for whatsapp only
    startActivity(Intent.createChooser(sharingIntent, "Share Image Using"));// for all generic options
}

最后在清单中添加此内容

Finaly in your manifest Add this without fail

        <activity
        android:name=".YourActivity"
        android:icon="@drawable/share_this"
        android:label="@string/shared_activity" >
        <intent-filter>
            <action android:name="android.intent.action.SEND" /> <!-- Send 
         action required to display activity in share list -->
            <category android:name="android.intent.category.DEFAULT" /> <!-- 
          Make activity default to launch -->
            <!-- Mime type i.e. what can be shared with this activity only image and text -->
            <data android:mimeType="image/*" />
            <data android:mimeType="text/*" />
        </intent-filter>
    </activity>

这应该欠

这篇关于使用Intent在什么应用上共享图像遇到错误共享失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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