云相册主界面activitr:
package com.michael.cloudphotos; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.drawerlayout.widget.DrawerLayout; import androidx.navigation.NavController; import androidx.navigation.Navigation; import androidx.navigation.ui.AppBarConfiguration; import androidx.navigation.ui.NavigationUI; import com.google.android.material.navigation.NavigationView; import com.luck.picture.lib.PictureSelector; import com.luck.picture.lib.config.PictureConfig; import com.luck.picture.lib.entity.LocalMedia; import com.michael.cloudphotos.User_Page.user_click_later_page; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.regex.Pattern; public class MainActivity extends AppCompatActivity { String count="0"; private String id="未登录"; private AppBarConfiguration mAppBarConfiguration; private ImageView mHead_portrait; public SQLhelper sqLhelper=new SQLhelper(MainActivity.this,"clouduser",null,1); helper p=new helper(sqLhelper); Handler handler=new Handler() { @Override public void handleMessage(@NonNull Message msg) { switch (msg.what) { case 1: { Pattern pattern = Pattern.compile("^[-\+]?[\d]*$"); if(pattern.matcher(msg.obj.toString()).matches()){ count=msg.obj.toString(); } break; } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Home_Fragment Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); start(); DrawerLayout drawer = findViewById(R.id.drawer_layout); NavigationView navigationView = findViewById(R.id.nav_view);//获取navigationView // Passing each menu ID as a set of Ids because each // menu should be considered as top level destinations. mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_home).setDrawerLayout(drawer).build(); NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration); NavigationUI.setupWithNavController(navigationView, navController); //退出功能 Menu menu=navigationView.getMenu();//获取NavigationView的子布局item对象 navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { switch (menuItem.getItemId()){ case R.id.nav_exit: Exit.getInstance().exit(); break; } return false; } }); //头像——跳转登录界面 View headView=navigationView.getHeaderView(0); ImageView mHead_portrait= headView.findViewById(R.id.Head_portrait);//通过headView来获取头布局中的组件 TextView userid=headView.findViewById(R.id.User_Name); userid.setText("用户名:"+id); mHead_portrait.setOnClickListener(new View.OnClickListener() { //如果有用户数据说明已经自动登录,没有则跳转到登录界面 @Override public void onClick(View v) { if(!p.lastuser()) { Intent intent = new Intent(MainActivity.this, HeadPortraitActivity.class); startActivity(intent); }else { //跳转到用户界面 Intent intent = new Intent(MainActivity.this, user_click_later_page.class); intent.putExtra("id",id); startActivity(intent); } } }); } public void start() //初始化 { if(p.lastuser()) { id=p.lastid(); //获取已存在的用户信息 } String time = new SimpleDateFormat("yyyy/MM/dd").format(new Date()); p.getcount(id,time,handler); //用于自动生成照片ID } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onSupportNavigateUp() { NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); return NavigationUI.navigateUp(navController, mAppBarConfiguration) || super.onSupportNavigateUp(); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { //相册选择照片后调用的回调函数。保存照片ID+上传照片 super.onActivityResult(requestCode, resultCode, data); String time = new SimpleDateFormat("yyyy/MM/dd").format(new Date()); if (resultCode == RESULT_OK) { switch (requestCode) { case PictureConfig.CHOOSE_REQUEST: List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data); for(LocalMedia i:selectList) { count=String.valueOf(Integer.valueOf(count)+1); p.updataphotoid(id+"-"+time+"-"+count,id,time,handler); p.uploadImage(i.getPath(),id+"-"+time+"-"+count,MainActivity.this); MainActivity.this.recreate(); } break; case PictureConfig.CAMERA: List<LocalMedia> a = PictureSelector.obtainMultipleResult(data); Toast.makeText(MainActivity.this,a.get(0).getPath(),Toast.LENGTH_SHORT).show(); Log.i("??",a.get(0).getPath()); break; } } } @Override protected void onStart() { super.onStart(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (((keyCode == KeyEvent.KEYCODE_BACK) || (keyCode == KeyEvent.KEYCODE_HOME)) && event.getRepeatCount() == 0) { dialog_Exit(MainActivity.this); } return false; } private void dialog_Exit(Context context) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage("确定要退出吗?"); builder.setTitle("提示"); builder.setIcon(android.R.drawable.ic_dialog_alert); builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); System.exit(0); finish(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } }