我正在制作一个应用程序,其中包含放置在 gridview 中的图像集合。现在我的问题是如何为其添加共享按钮。我搜索了它但无法成功实施?
问问题
1284 次
3 回答
1
我不知道您尝试过什么,但您确实应该查看开发人员指南以了解共享操作。它有一个关于如何在 android 中实现“分享按钮”的完整指南。
于 2014-08-15T07:11:45.443 回答
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!”);
于 2014-08-26T16:59:37.807 回答
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 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);
}
于 2018-03-30T07:41:56.340 回答