功能实现 :
Activiyt :
package com.lvche.lvchedingdang.activity.api_calling; import android.app.Activity; import android.app.AlertDialog; import android.content.ContentValues; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Toast; import com.lvche.lvcheappframelib.frame.common_utils.LogUtils; import com.lvche.lvcheappframelib.frame.common_utils.Utils; import com.lvche.lvchedingdang.R; import com.lvche.lvchedingdang.activity.BaseActivity; import com.lvche.lvchedingdang.activity.LBS.BaiduMapActivity; import com.lvche.lvchedingdang.activity.Msc.voicedemo.IatMscActivity; import com.lvche.lvchedingdang.activity.api_calling.adapter.APIGridAdapter; import com.lvche.lvchedingdang.activity.common_nav.fragment.view.MyGridView; import com.lvche.lvchedingdang.activity.contact.ContactMainActivity; import com.lvche.lvchedingdang.activity.record_vedio.com.sli.record.videomanager.VideoManager; import com.lvche.lvchedingdang.activity.shake.ShakeActivity; import com.lvche.lvchedingdang.activity.signal.yi_biao_pan.TestSpeedActivity; import com.lvche.lvchedingdang.views.custom_toast.MyToast; import com.zxing.MipcaActivityCapture; import java.io.File; /** * 手机API调用 */ public class APICallingActivity extends BaseActivity implements PhotoShop{ private MyGridView gridview; private final static int SCANNIN_GREQUEST_CODE = 1; /** 使用照相机拍照获取图片 */ protected static final int SELECT_PIC_BY_TACK_PHOTO = 1; /** 使用相册中的图片 */ protected static final int SELECT_PIC_BY_PICK_PHOTO = 2; private Uri photoUri; private String picPath; private boolean isCutPic; private String PHONE_NUM = "13800138000"; /** 地图定位 */ private static final int REQUEST_CODE_MAP = 4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_api_calling); initGridView(); } //初始化 gridview void initGridView() { gridview = (MyGridView)findViewById(R.id.gridview); gridview.setAdapter(new APIGridAdapter(this)); gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(); switch ((int) id) { case 0: /**图片上传等处理**/ selectPhoto(); break; case 1: /** *扫描二维码 */ intent.setClass(APICallingActivity.this, MipcaActivityCapture.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivityForResult(intent, SCANNIN_GREQUEST_CODE); break; case 2: //拨打电话 if (PHONE_NUM != null) { Intent it = new Intent(); it.setAction(Intent.ACTION_CALL); it.setData(Uri.parse("tel:" + PHONE_NUM.trim())); startActivity(it); } break; case 3: //LBS //MyToast.show(getApplicationContext(),"LBS功能正在完善中。。。",Toast.LENGTH_SHORT); startActivityForResult(new Intent(APICallingActivity.this, BaiduMapActivity.class), REQUEST_CODE_MAP); break; case 4: //摇一摇 ///MyToast.show(getApplicationContext(),"摇一摇功能正在完善中。。。",Toast.LENGTH_SHORT); startActivity(new Intent(APICallingActivity.this, ShakeActivity.class)); break; case 5: //网络状态监测 startActivity(new Intent(APICallingActivity.this, TestSpeedActivity.class)); //MyToast.show(getApplicationContext(),"网络状态监测功能正在完善中。。。",Toast.LENGTH_SHORT); break; case 6: //语音输入 //MyToast.show(getApplicationContext(),"语音输入功能正在完善中。。。",Toast.LENGTH_SHORT); startActivity(new Intent(APICallingActivity.this, IatMscActivity.class)); break; case 7: //视频录制 //MyToast.show(getApplicationContext(),"视频录制功能正在完善中。。。",Toast.LENGTH_SHORT); startActivity(new Intent(APICallingActivity.this, VideoManager.class)); break; case 8: //通讯录读取 //MyToast.show(getApplicationContext(),"通讯录读取功能正在完善中。。。",Toast.LENGTH_SHORT); startActivity(new Intent(APICallingActivity.this, ContactMainActivity.class)); break; } } }); } /*扫描器接收结果*/ /* @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case SCANNIN_GREQUEST_CODE: if(resultCode == RESULT_OK){ Bundle bundle = data.getExtras(); //显示扫描到的内容 //mTextView.setText(bundle.getString("result")); //显示 //mImageView.setImageBitmap((Bitmap) data.getParcelableExtra("bitmap")); Toast.makeText(APICallingActivity.this,"扫描 :" + bundle.getString("result"),Toast.LENGTH_LONG).show(); } break; } }*/ /**上传时,选择获取图片的方式 * */ public void selectPhoto() { // 弹出对话框选择 String datas[] = { "拍照获取照片", "从相册中选择照片" }; AlertDialog.Builder dataDialogBuidler = new AlertDialog.Builder(this).setTitle("选择图片").setItems(datas, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { if (item == 0) { // 拍照获取照片 // 开启相机 takePhoto(); } else { // 开启图册 pickPhoto(); } dialog.dismiss(); } }); dataDialogBuidler.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { dialog.dismiss(); } }); AlertDialog dialog = dataDialogBuidler.create(); dialog.setCancelable(true); dialog.show(); } /** * 拍照获取图片 */ public void takePhoto() { // 执行拍照前,应该先判断SD卡是否存在 String SDState = Environment.getExternalStorageState(); if (SDState.equals(Environment.MEDIA_MOUNTED)) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// "android.media.action.IMAGE_CAPTURE" /*** * 需要说明一下,以下操作使用照相机拍照,拍照后的图片会存放在相册中的 这里使用的这种方式有一个好处就是获取的图片是拍照后的原图 * 如果不实用ContentValues存放照片路径的话,拍照后获取的图片为缩略图不清晰 */ ContentValues values = new ContentValues(); photoUri = this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri); startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO); } else { MyToast.show(getApplicationContext(), "内存卡不存在", Toast.LENGTH_SHORT); } } /*** * 从相册中取图片 */ public void pickPhoto() { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { try{ if (isCutPic) { /**切图**/ cropPhoto(requestCode, resultCode, data); } else if (requestCode == REQUEST_CODE_MAP) { // 地图定位 locationResult(data); } else { /**处理接收图片结果*/ doPhoto(requestCode, data); } } catch(Exception e) { } super.onActivityResult(requestCode, resultCode, data); } /**切图**/ private void cropPhoto(int requestCode, int resultCode, Intent data) { isCutPic = false; if (resultCode == Activity.RESULT_OK) { //剪切上传 if (requestCode == 3) { Bundle extras = data.getExtras(); if (extras != null) { Bitmap bitmap = extras.getParcelable("data"); showImage(bitmap); } //doPhoto(requestCode, data); } else { try { if (requestCode == SELECT_PIC_BY_PICK_PHOTO) { /** 使用相册中的图片 */ photoUri = data.getData(); String photoAbsolutURI = Utils.getImageAbsolutePath(this, photoUri); Uri uri = Uri.fromFile(new File(photoAbsolutURI)); startPhotoZoom(uri); } else { /** 使用相机中的图片 */ String[] pojo = {MediaStore.Images.Media.DATA}; Cursor cursor = managedQuery(photoUri, pojo, null, null, null); if (cursor != null) { int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]); cursor.moveToFirst(); picPath = cursor.getString(columnIndex); //cursor.close(); } if (picPath != null && (picPath.toLowerCase().endsWith(".png") || picPath.toLowerCase().endsWith(".png") || picPath.toLowerCase().endsWith(".jpg") || picPath.toLowerCase().endsWith(".jpg"))) { startPhotoZoom(Uri.fromFile(new File(picPath))); } } } catch (Exception e) { MyToast.show(getApplicationContext(), "温馨提示:拾取照片失败,请重新选择!", Toast.LENGTH_SHORT); } } } } /**地图定位*/ private void locationResult(Intent data) { //纬度 double latitude = data.getDoubleExtra("latitude", 0); //经度 double longitude = data.getDoubleExtra("longitude", 0); String locationAddress = data.getStringExtra("address"); if (locationAddress != null && !locationAddress.equals("")) { //toggleMore(more); //sendLocationMsg(latitude, longitude, "", locationAddress); MyToast.show(getApplicationContext(), "您当前的位置 :" + locationAddress + " 经纬度: " + longitude + "," + latitude + "(" + ")", Toast.LENGTH_LONG); } else { String st = getResources().getString(R.string.unable_to_get_loaction); MyToast.show(this, st, Toast.LENGTH_SHORT); } } /** * 裁剪图片方法实现 * * @param uri */ public void startPhotoZoom(Uri uri ) { /* * 至于下面这个Intent的ACTION是怎么知道的,大家可以看下自己路径下的如下网页 * yourself_sdk_path/docs/reference/android/content/Intent.html * 直接在里面Ctrl+F搜:CROP ,之前没仔细看过,其实安卓系统早已经有自带图片裁剪功能, 是直接调本地库的 */ Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪 intent.putExtra("crop", "true"); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY 是裁剪图片宽高 intent.putExtra("outputX", 150); intent.putExtra("outputY", 150); intent.putExtra("return-data", true); //dat=file:///storage/emulated/0/DCIM/Camera/IMG_20151115_185459.jpg typ=image/jpeg this.startActivityForResult(intent, 3); } /** * 选择图片后,获取图片的路径 * * @param requestCode * @param data */ public void doPhoto(int requestCode, Intent data) { try { if (requestCode == SELECT_PIC_BY_PICK_PHOTO) { if (data == null) { MyToast.show(getApplicationContext(), "选择图片文件出错", Toast.LENGTH_SHORT); return; } photoUri = data.getData(); if (photoUri == null) { MyToast.show(getApplicationContext(), "选择图片文件出错", Toast.LENGTH_SHORT); return; } else { /**从相册取图片,有些手机有异常情况,请注意* */ LogUtils.i("上传回调结果onActivityResult URI:",photoUri.toString() ); // Bitmap bitmap = BitmapFactory.decodeFile(photoUri.toString()); try { // 读取uri所在的图片 // Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri); String photoAbsolutURI = Utils.getImageAbsolutePath(this, photoUri); final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(photoAbsolutURI,options); options.inSampleSize = Utils.calculateInSampleSize(options, 480, 800); options.inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory.decodeFile(photoAbsolutURI,options); showImage(bitmap); return; }catch (Exception e){ LogUtils.e("[Android]", e.getMessage()); LogUtils.e("[Android]", "目录为:" + photoUri); return; } } } /* 拍照获取图片 进行处理 * */ String[] pojo = {MediaStore.Images.Media.DATA}; Cursor cursor = managedQuery(photoUri, pojo, null, null, null); if (cursor != null) { int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]); cursor.moveToFirst(); picPath = cursor.getString(columnIndex); //cursor.close(); } if (picPath != null && (picPath.toLowerCase().endsWith(".png") || picPath.toLowerCase().endsWith(".png") || picPath.toLowerCase().endsWith(".jpg") || picPath.toLowerCase().endsWith(".jpg"))) { try { // 读取uri所在的图片 // Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri); String photoAbsolutURI = picPath; final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(photoAbsolutURI,options); options.inSampleSize = Utils.calculateInSampleSize(options, 480, 800); options.inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory.decodeFile(photoAbsolutURI,options); showImage(bitmap); }catch (Exception e){ LogUtils.e("[Android]", e.getMessage()); LogUtils.e("[Android]", "目录为:" + photoUri); } } else { MyToast.show(getApplicationContext(), "选择图片文件不正确", Toast.LENGTH_SHORT); } } catch (Exception e) { MyToast.show(getApplicationContext(), "温馨提示:拾取照片失败,请重新选择!",Toast.LENGTH_SHORT); } } public void showImage(Bitmap bitmap) { if (bitmap == null) { MyToast.show(getApplicationContext(),"图片似乎不正确~!!",Toast.LENGTH_SHORT); return; } LayoutInflater inflater=getLayoutInflater(); //将布局文件转换成相应的View对象 View layout=inflater.inflate(R.layout.toast_upload_img,(ViewGroup)findViewById(R.id.toast_layout_root)); //从layout中按照id查找imageView对象 ImageView imageView = (ImageView)layout.findViewById(R.id.upload_img); //设置ImageView的图片 imageView.setImageBitmap(bitmap); int scaleHeight = bitmap.getHeight() * 200 / bitmap.getWidth(); imageView.setLayoutParams(new LinearLayout.LayoutParams(200,scaleHeight)); //实例化一个Toast对象 Toast toast=new Toast(getApplicationContext()); toast.setDuration(Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setView(layout); toast.show(); } public void toGoBack(View view) { this.finish(); } }
toast操作部分 :
public void showImage(Bitmap bitmap) { if (bitmap == null) { MyToast.show(getApplicationContext(),"图片似乎不正确~!!",Toast.LENGTH_SHORT); return; } LayoutInflater inflater=getLayoutInflater(); //将布局文件转换成相应的View对象 View layout=inflater.inflate(R.layout.toast_upload_img,(ViewGroup)findViewById(R.id.toast_layout_root)); //从layout中按照id查找imageView对象 ImageView imageView = (ImageView)layout.findViewById(R.id.upload_img); //设置ImageView的图片 imageView.setImageBitmap(bitmap); int scaleHeight = bitmap.getHeight() * 200 / bitmap.getWidth(); imageView.setLayoutParams(new LinearLayout.LayoutParams(200,scaleHeight)); //实例化一个Toast对象 Toast toast=new Toast(getApplicationContext()); toast.setDuration(Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setView(layout); toast.show(); }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:gravity="center" android:id="@+id/toast_layout_root" android:layout_height="match_parent"> <ImageView android:id="@+id/upload_img" android:background="@drawable/toast_custom_border" android:layout_width="wrap_content" android:scaleType="fitXY" android:src="@drawable/icon_nav_icons" android:layout_height="wrap_content"/> </LinearLayout>
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#ffffff" /> <stroke android:width="1dp" android:color="#AAA5A5" /> <padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" /> </shape>