android actionbar中share使用、图片详细信息获取

来源:互联网 发布:期货模拟训练软件 编辑:程序博客网 时间:2024/04/29 09:55

效果图和源码:

   


public class DisplayPhoto extends Activity {private Context context;private String filePath;private Map<String, String> mapDetails;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);context = this;setContentView(R.layout.photo);ImageView img = (ImageView) findViewById(R.id.img);Uri uri = getIntent().getData();filePath = uri.getPath();if ("content".equals(uri.getScheme())) {filePath = getDataColumn(uri, null, null);} else if ("file".equals(uri.getScheme())) {}initDetails();Options o = new Options();o.inJustDecodeBounds = true;Bitmap bm = BitmapFactory.decodeFile(filePath, o);o.inSampleSize = 5;o.inJustDecodeBounds = false;bm = BitmapFactory.decodeFile(filePath, o);// Bitmap bm = BitmapFactory.decodeFile(filePath);img.setImageBitmap(bm);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// super.onCreateOptionsMenu(menu);getMenuInflater().inflate(R.menu.photo, menu);// share功能MenuItem share = menu.findItem(R.id.action_share);ShareActionProvider shareActionProvider = (ShareActionProvider) share.getActionProvider();if (shareActionProvider != null) {Intent intent = new Intent(Intent.ACTION_SEND);intent.setType(mapDetails.get(getString(R.string.mimetype)));intent.putExtra(Intent.EXTRA_STREAM, getIntent().getData());shareActionProvider.setShareIntent(intent);}return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()) {case R.id.action_confirm_delete:getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,MediaStore.Images.ImageColumns.DATA + "=?",new String[] { filePath });finish();break;case R.id.action_details:showDetails();break;case R.id.action_setas:Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);intent.setDataAndType(getIntent().getData(),mapDetails.get(getString(R.string.mimetype)));startActivity(Intent.createChooser(intent,this.getString(R.string.set_as)));break;default:break;}return super.onOptionsItemSelected(item);}/** * 根据contentprovider地址获取图片的真实路径 *  * @param uri *            contentprovider地址(以content:///开头的uri) * @param selection *            条件参数名 * @param selectionArgs *            条件参数值 * @return 真实路径 */private String getDataColumn(Uri uri, String selection,String[] selectionArgs) {Cursor cursor = null;String column = MediaStore.Images.ImageColumns.DATA;final String[] projection = { column };try {cursor = getContentResolver().query(uri, projection, selection,selectionArgs, null);if (cursor != null && cursor.moveToFirst()) {return cursor.getString(cursor.getColumnIndexOrThrow(column));}} finally {if (cursor != null)cursor.close();}return null;}private void initDetails() {Cursor cursor = null;String column = MediaStore.Images.ImageColumns.DATA;try {cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null,column + "=?", new String[] { filePath }, null);if (cursor != null && cursor.moveToFirst()) {mapDetails = new LinkedHashMap<String, String>();mapDetails.put(getString(R.string.title),cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME)));SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); // 实例化模板对象try {Date times = sdf.parse(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED)));mapDetails.put(getString(R.string.times),String.valueOf(times));} catch (ParseException e1) {e1.printStackTrace();}mapDetails.put(getString(R.string.description),cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DESCRIPTION)));int fileSize = Integer.parseInt(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.SIZE)));mapDetails.put(getString(R.string.file_size),formatFileSize(fileSize));mapDetails.put(getString(R.string.mimetype),cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.MIME_TYPE)));mapDetails.put(getString(R.string.height),cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.HEIGHT)));mapDetails.put(getString(R.string.width), cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.WIDTH)));try {ExifInterface exifInterface = new ExifInterface(filePath);String flash = exifInterface.getAttribute(ExifInterface.TAG_FLASH); // 闪光灯mapDetails.put(getString(R.string.flash), flash);String make = exifInterface.getAttribute(ExifInterface.TAG_MAKE); // 制造商mapDetails.put(getString(R.string.maker), make);String iso = exifInterface.getAttribute(ExifInterface.TAG_ISO); // ISOmapDetails.put(getString(R.string.iso), iso);String focalLength = exifInterface.getAttribute(ExifInterface.TAG_FOCAL_LENGTH);// 焦距mapDetails.put(getString(R.string.focal_length),focalLength);String exposureTime = exifInterface.getAttribute(ExifInterface.TAG_EXPOSURE_TIME);// 曝光时间mapDetails.put(getString(R.string.exposure_time),exposureTime);String whiteBalance = exifInterface.getAttribute(ExifInterface.TAG_WHITE_BALANCE);// 白平衡mapDetails.put(getString(R.string.white_balance),whiteBalance);String aperture = exifInterface.getAttribute(ExifInterface.TAG_APERTURE); // 光圈mapDetails.put(getString(R.string.aperture), aperture);String model = exifInterface.getAttribute(ExifInterface.TAG_MODEL); // 模型mapDetails.put(getString(R.string.model), model);} catch (IOException e) {e.printStackTrace();}mapDetails.put(getString(R.string.path), cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)));}} finally {if (cursor != null)cursor.close();}}/** * 转换文件大小 *  * @param fileS * @return */public String formatFileSize(int fileS) {DecimalFormat df = new DecimalFormat("#.00");String fileSizeString = "";if (fileS < 1024) {fileSizeString = df.format((double) fileS) + "B";} else if (fileS < 1048576) {fileSizeString = df.format((double) fileS / 1024) + "K";} else if (fileS < 1073741824) {fileSizeString = df.format((double) fileS / 1048576) + "M";} else {fileSizeString = df.format((double) fileS / 1073741824) + "G";}return fileSizeString;}private void showDetails() {new AlertDialog.Builder(context).setTitle("详细信息").setMessage(detailsToString()).setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}}).show();}public String detailsToString() {StringBuilder sb = new StringBuilder();Set<String> keys = mapDetails.keySet();Iterator<String> it = keys.iterator();while (it.hasNext()) {String key = it.next();if (mapDetails.get(key) != null && !"".equals(mapDetails.get(key))) {sb.append((key + ":" + mapDetails.get(key)) + "\n");}}sb.substring(0, sb.length() - 1);return sb.toString();}}



0 0