使用Intent共享图像什么是应用程序获取错误共享失败

时间:2018-02-27 14:37:36

标签: java android xml android-intent android-fileprovider

我从清单中的可绘制文件夹设置文件提供程序权限加载单个jpg图像但是当我在whatsapp上共享图像时我得到错误分享失败请再试一次。当我共享文本时它工作正常但是当我尝试共享图像时它给出了我的错误。这是关注文件

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>

2 个答案:

答案 0 :(得分:2)

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
}

最后在你的清单中添加此内容

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

这将是owrk

答案 1 :(得分:0)

ArrayList<Uri> uriArrayList = new ArrayList<>();
uriArrayList.add(getUriFromFile(your file path))
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);

intent.setType("image/*");
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, myFilesUriList);
startActivity(intent);


public Uri getUriFromFile(File theSrcPath) {
        Uri requirdUri = null;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            requirdUri = FileProvider.getUriForFile(theCtx,
                    theCtx.getApplicationContext().getPackageName() + PROVIDER_FILE_EXTENSION,
                    theSrcPath);
        } else {
            requirdUri = Uri.fromFile(theSrcPath);
        }

}