MainActivity
package com.example.share;
import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.share.adapter.MyAdapter;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements UMShareListener {
private TextView textShare;
private ListView lvList;
private MyAdapter adapter;
private List<String> imageList = new ArrayList<>();
private TextView tvCamera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= 23) {
String[] mPermissionList = new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS};
ActivityCompat.requestPermissions(this, mPermissionList, 123);
}
initView();
share();
button();
}
private Uri uri;
private String file;
private void button() {
tvCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
file = "/sdcard/DCIM/Camera/a.jpg";
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
uri = FileProvider.getUriForFile(MainActivity.this, "com.example.share", new File(file));
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
startActivityForResult(intent,101);
}
});
}
private void share() {
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String data = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
imageList.add(data);
}
adapter.notifyDataSetChanged();
}
lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "" + imageList.get(position), Toast.LENGTH_SHORT).show();
UMImage umImage = new UMImage(MainActivity.this, new File(imageList.get(position)));
new ShareAction(MainActivity.this)
.withText("hello")
.withMedia(umImage)
.setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.QZONE)
.setCallback(MainActivity.this).open();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101 && resultCode == Activity.RESULT_OK) {
UMImage umImage = new UMImage(MainActivity.this, new File(file));
new ShareAction(MainActivity.this)
.withText("hello")
.withMedia(umImage)
.setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.QZONE)
.setCallback(MainActivity.this).open();
}
UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
}
private void initView() {
textShare = findViewById(R.id.text_share);
adapter = new MyAdapter(imageList, this);
lvList = findViewById(R.id.lv_list);
lvList.setAdapter(adapter);
tvCamera = findViewById(R.id.tv_camera);
}
@Override
public void onStart(SHARE_MEDIA share_media) {
Toast.makeText(this, "开始分享", Toast.LENGTH_SHORT).show();
}
@Override
public void onResult(SHARE_MEDIA share_media) {
Toast.makeText(this, "分享成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(SHARE_MEDIA share_media, Throwable throwable) {
Toast.makeText(this, "分享失败" + throwable.getMessage(), Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel(SHARE_MEDIA share_media) {
Toast.makeText(this, "取消分享", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPause() {
super.onPause();
MobclickAgent.onResume(this);
}
@Override
protected void onResume() {
super.onResume();
MobclickAgent.onPause(this);
}
}
app
package com.example.share;
import android.app.Application;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
UMConfigure.init(this,"5dd233d54ca357255200000d"
,"umeng",UMConfigure.DEVICE_TYPE_PHONE,"d97d88f9c3f0a57fa15d8719d49db69f");
PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
PlatformConfig.setYixin("yxc0614e80c9304c11b0391514d09f13bf");
PlatformConfig.setWeixin("wxdc1e388c3822c80b", "3baf1193c85774b3fd9d18447d76cab0");
PlatformConfig.setSinaWeibo("3921700954", "04b48b094faeb16683c32669824ebdad","http://sns.whalecloud.com");
}
}