如何在我的Android应用程序中添加共享按钮?

时间:2014-08-15 07:09:33

标签: android share-button

我正在制作一个应用程序,其中包含放置在gridview中的图像集合。现在我的问题是如何添加一个共享按钮。我搜索它但无法成功实现它?

3 个答案:

答案 0 :(得分:1)

我不知道你尝试了什么,但你真的应该看看developer guides for share action。它有一个完整的工作指南,介绍如何实现"分享按钮"在android。

答案 1 :(得分:0)

试试这个http://developer.android.com/training/sharing/shareaction.html

或添加意图

Intent intent=new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

// Add data to the intent, the receiving app will decide what to do with it.
intent.putExtra(Intent.EXTRA_SUBJECT, “Some Subject Line”);
intent.putExtra(Intent.EXTRA_TEXT, “Body of the message, woot!”);

答案 2 :(得分:0)

通过键入以下命令,您可以在Android应用程序中添加共享按钮。

.XML代码

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="19dp"
        android:layout_marginStart="19dp"
        android:onClick="Clicked"
        android:text="@string/it"
        android:id="@+id/button2" />

STRING CODE

<string name="it">Share</string>

JAVA代码

package in.android.example.birthdaycardapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    private EditText editText;

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

        editText = (EditText) findViewById(R.id.editText);

    }

    public void Clicked(View view) {
       /* ACTION_SEND: Deliver some data to someone else.
        createChooser (Intent target, CharSequence title): Here, target- The Intent that the user will be selecting an activity to perform.
            title- Optional title that will be displayed in the chooser.
        Intent.EXTRA_TEXT: A constant CharSequence that is associated with the Intent, used with ACTION_SEND to supply the literal data to be sent.
        */
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, editText.getText().toString());
        sendIntent.setType("text/plain");
        Intent.createChooser(sendIntent, "Share via");
        startActivity(sendIntent);
    }

    Button bSave = (Button) findViewById(R.id.bSave);
bSave.setOnClickListener(new View.OnClickListener()

    {
        saveImage(true);
        Toast.makeText(getApplicationContext(), "Image Saved", 0).show();
    });

    public void saveImage(boolean isNotifyAfterSave) {
        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();

        File file = new File(extStorageDirectory, "ic_launcher.PNG");
        outStream = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();

        //if isNotifyAfterSave = true, then it will show an NotificationAlert.
        if (isNotifyAfterSave) {
            Uri uriOfFile = Uri.fromFile(file);
            showNotification("Image Saved", "desc:ic_launcher.png", "IMAGE SAVED!", uriOfFile);
        }

    }

    public void showNotification(String title, String text, String ticker, Uri pathToImage) {

        NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(
                context);

        Intent intent = new Intent(Intent.ACTION_VIEW, pathToImage);
        intent.setDataAndType(pathToImage, "image/*");

        PendingIntent pIntent = PendingIntent.getActivity(
                context.getApplicationContext(), 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        nBuilder.setContentText(text);
        nBuilder.setContentTitle(title);
        nBuilder.setContentIntent(pIntent);
        nBuilder.setTicker(ticker);
        nBuilder.setAutoCancel(true);
        nBuilder.setDefaults(Notification.DEFAULT_ALL);
        nBuilder.setSmallIcon(R.drawable.ic_stat_notify);
    }