无法通过ShareActionProvider共享图像

时间:2018-05-27 11:27:49

标签: java android shareactionprovider

我首先尝试从Gallery中获取一个简单的图像,相应地更新我的图像视图,然后应用两种方法进行共享,一种是通过简单的共享按钮,另一种是使用ShareActionProvider。 Whatsapp(或任何其他应用程序)在使用Action Bar共享时崩溃,但与简单的共享按钮完美共享。

ShareActionProvider完美地共享文本但不共享图像。请帮忙

以下是我的菜单文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.android.shareactivity.ShareActivity">

<item
    android:title="@string/action_share"
    android:id="@+id/action_share"
    android:orderInCategory="2"
    app:showAsAction="always"
    app:actionProviderClass="android.support.v7.widget.ShareActionProvider"


    />

以下是我的ShareActivity.java

package com.example.android.shareactivity;
import java.io.FileNotFoundException;
import java.io.InputStream;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;

import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;




import com.example.android.shareactivity.R;

import static android.R.attr.bitmap;
import static android.R.attr.data;
import static android.R.id.shareText;
import static com.example.android.shareactivity.R.string.imageview;




public class ShareActivity extends AppCompatActivity implements OnClickListener {
      Button selectImage;
      ImageButton shareImage;
      ImageView imageView;
      private ShareActionProvider mShareActionProvider;


// Uri for image path
Uri imageUri = null;

private final int select_photo = 1; // request code for gallery intent

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.share_activity);

    init();
    setListeners();
}

// Initialize views
private void init() {
    selectImage = (Button) findViewById(R.id.select_image);
    shareImage = (ImageButton) findViewById(R.id.share_image);
    imageView = (ImageView) findViewById(R.id.share_imageview);


}

// Implement click listeners
private void setListeners() {
    selectImage.setOnClickListener(this);
    shareImage.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.select_image:
            // Intent to gallery
            Intent in = new Intent(Intent.ACTION_PICK);
            in.setType("image/*");
            startActivityForResult(in, select_photo);// start
            // activity
            // for
            // result
            break;
        case R.id.share_image:

            // share image
            shareImage(imageUri);

            break;


    }
}

protected void onActivityResult(int requestcode, int resultcode,
                                Intent imagereturnintent) {
    super.onActivityResult(requestcode, resultcode, imagereturnintent);
    switch (requestcode) {
        case select_photo:
            if (resultcode == RESULT_OK) {
                try {

                    imageUri = imagereturnintent.getData();// Get intent
                    // data

                    Bitmap bitmap = utils.decodeUri(ShareActivity.this,
                            imageUri, 200);
                    if (bitmap != null) {
                        imageView.setImageBitmap(bitmap);// Set image over image
                        shareImage.setVisibility(View.VISIBLE);
                    }


                    else {
                        shareImage.setVisibility(View.GONE);
                        Toast.makeText(ShareActivity.this,
                                "Error while decoding image.",
                                Toast.LENGTH_SHORT).show();
                    }
                } catch (FileNotFoundException e) {

                    e.printStackTrace();
                    Toast.makeText(ShareActivity.this, "File not found.",
                            Toast.LENGTH_SHORT).show();
                }
            }
    }
}




// Share image
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);
    startActivity(Intent.createChooser(sharingIntent, "Share Image Using"));

}


//Sharing through ShareActionButton

@Override

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    //Share Action Provider
    MenuItem menuItem = menu.findItem(R.id.action_share);
    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);


    if(mShareActionProvider!=null) {

//            myShareIntent.setType("text/*");
//           myShareIntent.putExtra(Intent.EXTRA_TEXT,"Hello World");
            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
            sharingIntent.setType("image/*");
            sharingIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
            mShareActionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
            mShareActionProvider.setShareIntent(sharingIntent);
        }

    return true;

}





}

以下是我用来获取位图图像的util函数

package com.example.android.shareactivity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

import java.io.FileNotFoundException;

/**
 * Created by Simratpal Singh on 5/26/2018.
 */

public class utils {
    public static Bitmap decodeUri(Context context, Uri uri,
                                   final int requiredSize) throws FileNotFoundException {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(context.getContentResolver()
                .openInputStream(uri), null, o);

        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;

        while (true) {
            if (width_tmp / 2 < requiredSize || height_tmp / 2 < requiredSize)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(context.getContentResolver()
                .openInputStream(uri), null, o2);
    }
}

0 个答案:

没有答案