安卓高级9 用原生intent分享
大家都用过安卓app时发现有个分享按钮如下:
所以今天特此分享用用原生完成:
package qianfeng.com.simplesharedemo;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import java.io.File;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
shareImage();
}
public void shareText() {
//点击分享
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "这是一条测试信息");
String title = " 分享到: ";
startActivity(Intent.createChooser(intent, title));
}
public void shareImage() {
String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "aaa.jpg";
Uri uri = Uri.fromFile(new File(filePath));
//点击分享
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, uri);
String title = " 分享到: ";
startActivity(Intent.createChooser(intent, title));
}
}
另外还有分享多个图片等案例
public void shareImage() {
String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "aaa.jpg";
String filePath2 = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "aaa.jpg";
Uri uri = Uri.fromFile(new File(filePath));
Uri uri2 = Uri.fromFile(new File(filePath2));
ArrayList<Uri> uris = new ArrayList<>();
uris.add(uri);
uris.add(uri2);
//点击分享
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
String title = " 分享到: ";
startActivity(Intent.createChooser(intent, title));
}