Compare commits

...

2 Commits

Author SHA1 Message Date
向金成 772acb4157 1
2 years ago
向金成 3fbca3c73a 8
2 years ago

@ -0,0 +1,70 @@
package net.micode.notes.data;
import android.content.Context;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
public class LoginData {
static String fileName = "LoginData";
private static HashMap<String,String> loginData = new HashMap<String,String >();
//获取登录数据
public HashMap<String,String> getLoginData(){
return loginData;
}
//注册时添加
public static void write(String account,String password,Context context) throws IOException {
FileOutputStream fos = null;
try {
//获取文件的输出流对象fos
fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
//将数据转换为字节码的形式写入data.txt文件中
fos.write((account + ":" + password).getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close(); //关闭输出流
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//读文件
public static void read (Context context){
String content = "";
FileInputStream fis = null;
try {
fis = context.openFileInput(fileName); //获取文件的输入流对象fis
byte[] buffer = new byte[fis.available()]; //获取文件长度,创建缓冲区,将输入流对象中的数据转换为字节码的形式
fis.read(buffer);//通过read()方法读取字节码中的数据读取到buffer缓冲区
content = new String(buffer); //将获取的字节码转换为字符串
String[] infos = content.split(":");//将字符串以“:”分隔后形成一个数组的形式
loginData.put(infos[0],infos[1]); //将数组中的第一个数据放入userMap集合中
//将数组中的第二个数据放入userMap集合中
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close(); //关闭输入流
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//添加到map
public static void addLoginData (String account, String password){
loginData.put(account, password);
}
}

@ -0,0 +1,33 @@
package net.micode.notes.tool;
import android.content.Context;
import net.micode.notes.data.LoginData;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class CheckUtils {
public LoginData loginData = new LoginData();
public CheckUtils(Context context) throws IOException, ClassNotFoundException {
loginData.read(context);
}
//验证
public boolean check(String account, String password){
HashMap<String,String> data = loginData.getLoginData();
Iterator<HashMap.Entry<String, String>> it = data.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String,String> entry = it.next();
String account1 = entry.getKey();
String password1 = entry.getValue();
if(account1.equals(account) && password1.equals(password)){
return true;
}
}
return false;
}
}

@ -0,0 +1,147 @@
package net.micode.notes.tool;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.util.Log;
import net.micode.notes.ui.NoteEditText;
public class PictureaddUtils {
public void convertToImage(NoteEditText noteEditText, String TAG, Context NoteEditActivity) {
//获取当前的edit
Editable editable = noteEditText.getText();//1.获取text
String noteText = editable.toString(); //2.将note内容转换为字符串
int length = editable.length(); //内容的长度
//3.截取img片段 [local]+uri+[local]提取uri
for(int i = 0; i < length; i++) {
for(int j = i; j < length; j++) {
String img_fragment = noteText.substring(i, j+1); //img_fragment关于图片路径的片段
if(img_fragment.length() > 15 && img_fragment.endsWith("[/local]") && img_fragment.startsWith("[local]")){
int limit = 7; //[local]为7个字符
//[local][/local]共15个字符剩下的为真正的path长度
int len = img_fragment.length()-15;
//从[local]之后的len个字符就是path
String path = img_fragment.substring(limit,limit+len);//获取到了图片路径
Bitmap bitmap = null;
Log.d(TAG, "图片的路径是:"+path);
try {
bitmap = BitmapFactory.decodeFile(path);//将图片路径解码为图片格式
} catch (Exception e) {
e.printStackTrace();
}
if(bitmap!=null){ //若图片存在
Log.d(TAG, "图片不为null");
ImageSpan imageSpan = new ImageSpan(NoteEditActivity, bitmap);
//4.创建一个SpannableString对象以便插入用ImageSpan对象封装的图像
String ss = "[local]" + path + "[/local]";
SpannableString spannableString = new SpannableString(ss);
//5.将指定的标记对象附加到文本的开始...结束范围
spannableString.setSpan(imageSpan, 0, ss.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Log.d(TAG, "Create spannable string success!");
Editable edit_text = noteEditText.getEditableText();
edit_text.delete(i,i+len+15); //6.删掉图片路径的文字
edit_text.insert(i, spannableString); //7.在路径的起始位置插入图片
}
}
}
}
}
//获取文件的real path
public String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
// if (isExternalStorageDocument(uri)) {
// final String docId = DocumentsContract.getDocumentId(uri);
// final String[] split = docId.split(":");
// final String type = split[0];
//
// if ("primary".equalsIgnoreCase(type)) {
// return Environment.getExternalStorageDirectory() + "/" + split[1];
// }
// }
// // DownloadsProvider
// else if (isDownloadsDocument(uri)) {
// final String id = DocumentsContract.getDocumentId(uri);
// final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
// return getDataColumn(context, contentUri, null, null);
// }
// MediaProvider
// else
if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{split[1]};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// Media
else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
//获取数据列_获取此 Uri 的数据列的值。这对MediaStore Uris 和其他基于文件的 ContentProvider。
public String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {column};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
//是否为外部存储文件
// public boolean isExternalStorageDocument(Uri uri) {
// return "com.android.externalstorage.documents".equals(uri.getAuthority());
// }
//
// //是否为下载文件
// public boolean isDownloadsDocument(Uri uri) {
// return "com.android.providers.downloads.documents".equals(uri.getAuthority());
// }
//是否为媒体文件
public boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
}

@ -0,0 +1,135 @@
package net.micode.notes.ui;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import net.micode.notes.R;
import net.micode.notes.tool.CheckUtils;
import java.io.IOException;
import java.util.Timer;
import java.util.zip.Inflater;
public class LoginActivity extends Activity {
String account;
String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.login);
//保存输入信息
EditText inputAccount = (EditText)findViewById(R.id.login_aacount);
EditText inputPassword = (EditText) findViewById(R.id.login_password);
inputAccount.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
account = s.toString();
}
});
inputPassword.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
password = s.toString();
}
});
//登录按钮
Button loginButton = (Button) findViewById(R.id.login_accept);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckUtils checkUtils = null;
try {
checkUtils = new CheckUtils(LoginActivity.this);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
if(checkUtils.check(account,password)){
Intent intent = new Intent(LoginActivity.this,NoteEditActivity.class);
intent.setAction(Intent.ACTION_VIEW);
startActivity(intent);
}
else{
Toast checkErrorToast = Toast.makeText(LoginActivity.this,R.string.check_error,Toast.LENGTH_SHORT);
checkErrorToast.show();
}
}
});
//注册按钮
Button registerButton = (Button) findViewById(R.id.login_register);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//输入非空
if(account != null && password != null) {
CheckUtils checkUtils = null;
try {
checkUtils = new CheckUtils(LoginActivity.this);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
try {
checkUtils.loginData.write(account,password,LoginActivity.this);
} catch (IOException e) {
throw new RuntimeException(e);
}
Toast registerOKToast = Toast.makeText(LoginActivity.this, R.string.register_ok, Toast.LENGTH_SHORT);
registerOKToast.show();
Intent intent = new Intent(LoginActivity.this, NoteEditActivity.class);
intent.setAction(Intent.ACTION_VIEW);
startActivity(intent);
}
else {
account = null;
password = null;
Toast toast = Toast.makeText(LoginActivity.this,R.string.input_error,Toast.LENGTH_SHORT);
toast.show();
}
}
});
}
}
Loading…
Cancel
Save