diff --git a/students/soft1714080902219/app/src/main/AndroidManifest.xml b/students/soft1714080902219/app/src/main/AndroidManifest.xml index 379cc3f9b..3fbb3c219 100644 --- a/students/soft1714080902219/app/src/main/AndroidManifest.xml +++ b/students/soft1714080902219/app/src/main/AndroidManifest.xml @@ -1,6 +1,17 @@ + + + + + + + + + tools:ignore="ProtectedPermissions"/> + + + + + + + @@ -16,9 +45,8 @@ - + + - - - + \ No newline at end of file diff --git a/students/soft1714080902219/app/src/main/java/edu/hzuapps/androidlabs/Soft1714080902219/PictureUtils.java b/students/soft1714080902219/app/src/main/java/edu/hzuapps/androidlabs/Soft1714080902219/PictureUtils.java new file mode 100644 index 000000000..bfb5e1ae2 --- /dev/null +++ b/students/soft1714080902219/app/src/main/java/edu/hzuapps/androidlabs/Soft1714080902219/PictureUtils.java @@ -0,0 +1,491 @@ +package edu.hzuapps.androidlabs.Soft1714080902219; + +import android.annotation.TargetApi; + +import android.content.ContentUris; + +import android.content.Context; + +import android.content.Intent; + +import android.database.Cursor; + +import android.graphics.Bitmap; + +import android.graphics.BitmapFactory; + +import android.net.Uri; + +import android.os.Build; + +import android.os.Environment; + +import android.provider.DocumentsContract; + +import android.provider.MediaStore; + + + +import java.io.File; + +import java.io.IOException; + + + +/** + + * 图片处理的工具类 + + */ + +public class PictureUtils { + + /** + + * API19以下获取图片路径的方法 + + * + + * @param uri + + */ + + public static String getFilePath_below19(Context context, Uri uri) { + + //这里开始的第二部分,获取图片的路径:低版本的是没问题的,但是sdk>19会获取不到 + + String[] proj = {MediaStore.Images.Media.DATA}; + + //好像是android多媒体数据库的封装接口,具体的看Android文档 + + Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null); + + //获得用户选择的图片的索引值 + + int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); + + //将光标移至开头 ,这个很重要,不小心很容易引起越界 + + cursor.moveToFirst(); + + //最后根据索引值获取图片路径 结果类似:/mnt/sdcard/DCIM/Camera/IMG_20151124_013332.jpg + + String path = cursor.getString(column_index); + + return path; + + } + + + + /** + + * APIlevel 19以上才有 + + * 创建项目时,我们设置了最低版本API Level,比如我的是10, + + * 因此,AS检查我调用的API后,发现版本号不能向低版本兼容, + + * 比如我用的“DocumentsContract.isDocumentUri(context, uri)”是Level 19 以上才有的, + + * 自然超过了10,所以提示错误。 + + * 添加 @TargetApi(Build.VERSION_CODES.KITKAT)即可。 + + * + + * @param context + + * @param uri + + * @return + + */ + + @TargetApi(Build.VERSION_CODES.KITKAT) + + public static String getPath_above19(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; + + } else if ("video".equals(type)) { + + contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; + + } else if ("audio".equals(type)) { + + contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; + + } + + final String selection = "_id=?"; + + final String[] selectionArgs = new String[]{ + + split[1] + + }; + + return getDataColumn(context, contentUri, selection, selectionArgs); + + } + + } + + // MediaStore (and general) + + else if ("content".equalsIgnoreCase(uri.getScheme())) { + + // Return the remote address + + if (isGooglePhotosUri(uri)) + + return uri.getLastPathSegment(); + + return getDataColumn(context, uri, null, null); + + } + + // File + + else if ("file".equalsIgnoreCase(uri.getScheme())) { + + return uri.getPath(); + + } + + return null; + + } + + + + /** + + * Get the value of the data column for this Uri. This is useful for + + * MediaStore Uris, and other file-based ContentProviders. + + * + + * @param context The context. + + * @param uri The Uri to query. + + * @param selection (Optional) Filter used in the query. + + * @param selectionArgs (Optional) Selection arguments used in the query. + + * @return The value of the _data column, which is typically a file path. + + */ + + public static 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 index = cursor.getColumnIndexOrThrow(column); + + return cursor.getString(index); + + } + + } finally { + + if (cursor != null) + + cursor.close(); + + } + + return null; + + } + + + + /** + + * @param uri The Uri to check. + + * @return Whether the Uri authority is ExternalStorageProvider. + + */ + + public static boolean isExternalStorageDocument(Uri uri) { + + return "com.android.externalstorage.documents".equals(uri.getAuthority()); + + } + + + + /** + + * @param uri The Uri to check. + + * @return Whether the Uri authority is DownloadsProvider. + + */ + + public static boolean isDownloadsDocument(Uri uri) { + + return "com.android.providers.downloads.documents".equals(uri.getAuthority()); + + } + + + + /** + + * @param uri The Uri to check. + + * @return Whether the Uri authority is MediaProvider. + + */ + + public static boolean isMediaDocument(Uri uri) { + + return "com.android.providers.media.documents".equals(uri.getAuthority()); + + } + + + + /** + + * @param uri The Uri to check. + + * @return Whether the Uri authority is Google Photos. + + */ + + public static boolean isGooglePhotosUri(Uri uri) { + + return "com.google.android.apps.photos.content".equals(uri.getAuthority()); + + } + + + + /** + + * 图片的压缩 + + * + + * @param options + + * @param reqWidth + + * @param reqHeight + + * @return + + */ + + public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { + + final int height = options.outHeight; + + final int width = options.outWidth; + + int inSampleSize = 1; + + if (height > reqHeight || width > reqWidth) { + + final int heightRatio = Math.round((float) height + + / (float) reqHeight); + + final int widthRatio = Math.round((float) width / (float) reqWidth); + + inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; + + } + + return inSampleSize; + + } + + + + /** + + * 根据路径获得突破并压缩返回bitmap用于显示 + + * + + * @param filePath + + * @return + + */ + + public static Bitmap getSmallBitmap(String filePath, int reqWidth, int reqHeight) { + + final BitmapFactory.Options options = new BitmapFactory.Options(); + + options.inJustDecodeBounds = true; //只返回图片的大小信息 + + BitmapFactory.decodeFile(filePath, options); + + // Calculate inSampleSize + + options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); + + // Decode bitmap with inSampleSize set + + options.inJustDecodeBounds = false; + + return BitmapFactory.decodeFile(filePath, options); + + } + + + + /** + + * 判断sdcard是否被挂载 + + * + + * @return + + */ + + public static boolean hasSdcard() { + + if (Environment.getExternalStorageState().equals( + + Environment.MEDIA_MOUNTED)) { + + return true; + + } else { + + return false; + + } + + } + + + + /** + + * 将照片添加到相册中 + + */ + + public static void galleryAddPic(String mPublicPhotoPath, Context context) { + + Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); + + File f = new File(mPublicPhotoPath); + + Uri contentUri = Uri.fromFile(f); + + mediaScanIntent.setData(contentUri); + + context.sendBroadcast(mediaScanIntent); + + } + + /** + + * 创建临时图片存储的路径 + + * + + * @return + + * @throws IOException + + */ + + public static File createPublicImageFile() throws IOException { + + File appDir = new File(Environment.getExternalStorageDirectory() + "/photodemo"); + + if (!appDir.exists()) { + + appDir.mkdir(); + + } + + String fileName = System.currentTimeMillis() + ".jpg"; + + File file = new File(appDir, fileName); + + return file; + + } + +} \ No newline at end of file diff --git a/students/soft1714080902219/app/src/main/java/edu/hzuapps/androidlabs/Soft1714080902219/Soft1714080902219Activity3.java b/students/soft1714080902219/app/src/main/java/edu/hzuapps/androidlabs/Soft1714080902219/Soft1714080902219Activity3.java index 9bbf78251..ce7e1f6a1 100644 --- a/students/soft1714080902219/app/src/main/java/edu/hzuapps/androidlabs/Soft1714080902219/Soft1714080902219Activity3.java +++ b/students/soft1714080902219/app/src/main/java/edu/hzuapps/androidlabs/Soft1714080902219/Soft1714080902219Activity3.java @@ -1,5 +1,6 @@ package edu.hzuapps.androidlabs.Soft1714080902219; +import android.content.Intent; import android.os.Handler; import android.os.Message; import android.os.StrictMode; @@ -47,7 +48,7 @@ public void handleMessage(Message msg) { public void getDataByNet(){ try { - String url_s = "https://raw.githubusercontent.com/wanshanghong/liulangzhe/master/myjson.json"; //我的阿里云的url:http://47.103.6.223:8080/liulangzhe-manager-web/myjson.json + String url_s = "https://raw.githubusercontent.com/wanshanghong/liulangzhe/master/myjson.json"; //raw得到的url:https://raw.githubusercontent.com/wanshanghong/liulangzhe/master/myjson.json URL url = new URL(url_s); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(500000); @@ -154,9 +155,9 @@ protected void onCreate(Bundle savedInstanceState) { Log.d("mylog","Soft1714080902219Activity3;运行"); fManager = getSupportFragmentManager(); - initViews(); setChioceItem(0); + } //完成组件的初始化 @@ -224,9 +225,14 @@ public void setChioceItem(int index) // 如果fg1为空,则创建一个并添加到界面上 fragment2 = new Soft1714080902219Fragment2(); transaction.add(R.id.main_content, fragment2); + } else { // 如果MessageFragment不为空,则直接将它显示出来 transaction.show(fragment2); + StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build()); + Log.d("mylog","Soft1714080902219Fragment2onCreateView;运行"); + listView =this.findViewById(R.id.listview); + getDataByNet(); } break; case 2: @@ -240,10 +246,8 @@ public void setChioceItem(int index) } else { // 如果MessageFragment不为空,则直接将它显示出来 transaction.show(fragment3); - StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build()); - Log.d("mylog","Soft1714080902219Fragment2onCreateView;运行"); - listView =this.findViewById(R.id.listview); - getDataByNet(); + + } break; } diff --git a/students/soft1714080902219/app/src/main/java/edu/hzuapps/androidlabs/Soft1714080902219/Soft1714080902219Fragment3.java b/students/soft1714080902219/app/src/main/java/edu/hzuapps/androidlabs/Soft1714080902219/Soft1714080902219Fragment3.java index abda50a5e..abf483f70 100644 --- a/students/soft1714080902219/app/src/main/java/edu/hzuapps/androidlabs/Soft1714080902219/Soft1714080902219Fragment3.java +++ b/students/soft1714080902219/app/src/main/java/edu/hzuapps/androidlabs/Soft1714080902219/Soft1714080902219Fragment3.java @@ -1,15 +1,43 @@ package edu.hzuapps.androidlabs.Soft1714080902219; +import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.LinearLayout; +import android.widget.TextView; public class Soft1714080902219Fragment3 extends Fragment { + private View view,view2; public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { - return inflater.inflate(R.layout.soft_1714080902219_fragment3,container,false); + view = inflater.inflate(R.layout.soft_1714080902219_fragment3,container,false); + + + return view; + + } + + @Override + public void onActivityCreated(@Nullable Bundle savedInstanceState) { + + super.onActivityCreated(savedInstanceState); + LinearLayout linearLayout=getActivity().findViewById(R.id.LinearLayout03); + linearLayout.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(getActivity(),Soft1714080902219TakephotoActivity.class); + startActivity(intent); + //Log.d("mylog","setOnClickListener;运行startActivity(intent);"); + + Log.d("mylog","setOnClickListener;运行"); + } + }); + + } } diff --git a/students/soft1714080902219/app/src/main/java/edu/hzuapps/androidlabs/Soft1714080902219/Soft1714080902219TakephotoActivity.java b/students/soft1714080902219/app/src/main/java/edu/hzuapps/androidlabs/Soft1714080902219/Soft1714080902219TakephotoActivity.java new file mode 100644 index 000000000..4f0c55b07 --- /dev/null +++ b/students/soft1714080902219/app/src/main/java/edu/hzuapps/androidlabs/Soft1714080902219/Soft1714080902219TakephotoActivity.java @@ -0,0 +1,280 @@ +package edu.hzuapps.androidlabs.Soft1714080902219; + + +import android.Manifest; +import android.app.Activity; +import android.content.Intent; +import android.net.Uri; +import android.os.Build; +import android.os.Bundle; +import android.provider.MediaStore; +import android.support.v4.content.FileProvider; +import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.widget.Button; +import android.widget.ImageView; + +import java.io.File; +import java.io.IOException; + +import kr.co.namee.permissiongen.PermissionFail; +import kr.co.namee.permissiongen.PermissionGen; +import kr.co.namee.permissiongen.PermissionSuccess; + +public class Soft1714080902219TakephotoActivity extends AppCompatActivity implements View.OnClickListener { + + private static final int SUCCESSCODE = 100; + + public Button mButton1; + + public Button mButton2; + + private String mPublicPhotoPath; + + private static final int REQ_GALLERY = 333; + + private static final int REQUEST_CODE_PICK_IMAGE = 222; + + public ImageView mImageView; + + + + @Override + + protected void onCreate(Bundle savedInstanceState) { + + super.onCreate(savedInstanceState); + + setContentView(R.layout.soft_1714080902219_takephoto_activity); + + mButton1 = ((Button) findViewById(R.id.bt1)); + + mButton2 = ((Button) findViewById(R.id.bt2)); + + mButton1.setOnClickListener(this); + + mButton2.setOnClickListener(this); + + mImageView = ((ImageView) findViewById(R.id.iv)); + + } + + + + @Override + + public void onClick(View v) { + + switch (v.getId()) { + + //获取相册中的照片 + + case R.id.bt1: + + getImageFromAlbum(); + + break; + + //拍照功能 + + case R.id.bt2: + + showTakePicture(); + + break; + + } + + } + + + + /** + + * 获取相册中的图片 + + */ + + public void getImageFromAlbum() { + + Intent intent = new Intent(Intent.ACTION_PICK); + + intent.setType("image/*");//相片类型 + + startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE); + + } + + + + //拍照的功能 + + private void showTakePicture() { + + PermissionGen.with(Soft1714080902219TakephotoActivity.this) + + .addRequestCode(SUCCESSCODE) + + .permissions( + + Manifest.permission.CAMERA, + + Manifest.permission.WRITE_EXTERNAL_STORAGE, + + Manifest.permission.READ_EXTERNAL_STORAGE + + ) + + .request(); + + } + + + + @Override + + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + + PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults); + + } + + + + //权限申请成功 + @PermissionSuccess(requestCode = SUCCESSCODE) + public void doSomething() { + + //申请成功 + + startTake(); + + } + + + + @PermissionFail(requestCode = SUCCESSCODE) + + public void doFailSomething() { + + } + + + + private void startTake() { + + Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + + //判断是否有相机应用 + + if (takePictureIntent.resolveActivity(getPackageManager()) != null) { + + //创建临时图片文件 + + File photoFile = null; + + try { + + photoFile = PictureUtils.createPublicImageFile(); + + mPublicPhotoPath = photoFile.getAbsolutePath(); + + } catch (IOException e) { + + e.printStackTrace(); + + } + + //设置Action为拍照 + + if (photoFile != null) { + + takePictureIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); + + //这里加入flag + + takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + + Uri photoURI = FileProvider.getUriForFile(this, "applicationId.fileprovider", photoFile); + + takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); + + startActivityForResult(takePictureIntent, REQ_GALLERY); + + } + + } + + } + + + + private Uri uri; + + String path; + + int mTargetW; + + int mTargetH; + + + + @Override + + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + + super.onActivityResult(requestCode, resultCode, data); + + mTargetW = mImageView.getWidth(); + + mTargetH = mImageView.getHeight(); + + + + switch (requestCode) { + + //拍照 + + case REQ_GALLERY: + + if (resultCode != Activity.RESULT_OK) return; + + uri = Uri.parse(mPublicPhotoPath); + + path = uri.getPath(); + + PictureUtils.galleryAddPic(mPublicPhotoPath, this); + + break; + + //获取相册的图片 + + case REQUEST_CODE_PICK_IMAGE: + + if (data == null) return; + + uri = data.getData(); + + int sdkVersion = Integer.valueOf(Build.VERSION.SDK); + + if (sdkVersion >= 19) { + + path = this.uri.getPath(); + + path = PictureUtils.getPath_above19(Soft1714080902219TakephotoActivity.this, this.uri); + + } else { + + path = PictureUtils.getFilePath_below19(Soft1714080902219TakephotoActivity.this, this.uri); + + } + + break; + + } + + mImageView.setImageBitmap(PictureUtils.getSmallBitmap(path, mTargetW, mTargetH)); + + } +} + diff --git a/students/soft1714080902219/app/src/main/res/layout/soft_1714080902219_fragment3.xml b/students/soft1714080902219/app/src/main/res/layout/soft_1714080902219_fragment3.xml index 57ececb63..14c4b7b75 100644 --- a/students/soft1714080902219/app/src/main/res/layout/soft_1714080902219_fragment3.xml +++ b/students/soft1714080902219/app/src/main/res/layout/soft_1714080902219_fragment3.xml @@ -12,6 +12,8 @@ android:background="#a7d9e7"> + + + - + + + + + + + + + + + diff --git a/students/soft1714080902219/app/src/main/res/layout/soft_1714080902219_takephoto_activity.xml b/students/soft1714080902219/app/src/main/res/layout/soft_1714080902219_takephoto_activity.xml new file mode 100644 index 000000000..ca21594a7 --- /dev/null +++ b/students/soft1714080902219/app/src/main/res/layout/soft_1714080902219_takephoto_activity.xml @@ -0,0 +1,46 @@ + + + +