public static Bitmap getScaleOptionImageByScaleOfWinWidth( Context context, File imgFile, float scaleOfWinWidth) { if (imgFile == null) return null; BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = true; BitmapFactory.decodeFile(imgFile.getAbsolutePath(), bmOptions); int photoW = bmOptions.outWidth; int photoH = bmOptions.outHeight; if (photoW == 0) { return null; } int th = (int) (DeviceUtil.getDeviceHeight(context)); int tw = (int) (DeviceUtil.getDeviceWidth(context)); tw = (int) (tw * scaleOfWinWidth); if (tw == 0) { return null; } th = (int) (tw * ((double) photoH / photoW)); int scaleFactor = Math.min(photoW / tw, photoH / th); bmOptions.inJustDecodeBounds = false; bmOptions.inSampleSize = scaleFactor; bmOptions.inPurgeable = true; Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), bmOptions); return bitmap; }
public static EMImageSpan getEmojSpan(Context context, String emojKey) { int resId = context.getResources().getIdentifier(emojKey, "drawable", context.getPackageName()); Drawable emojDrwable = context.getResources().getDrawable(resId); emojDrwable.setBounds(0, 0, DeviceUtil.getSpanEmojSize(), DeviceUtil.getSpanEmojSize()); EMImageSpan imgSpan = new EMImageSpan(emojDrwable); imgSpan.mTransferTxt = "#[" + emojKey + "]"; return imgSpan; }
public static void addEmail(Context c, long rawContactId, String email) { DeviceUtil.log(c, "adding email", email); String where = ContactsContract.Data.RAW_CONTACT_ID + " = '" + rawContactId + "' AND " + ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE + "'"; Cursor cursor = c.getContentResolver() .query( ContactsContract.Data.CONTENT_URI, new String[] {RawContacts.CONTACT_ID}, where, null, null); if (cursor.getCount() == 0) { ContentValues contentValues = new ContentValues(); // op.put(ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID, ); contentValues.put( ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE); contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); contentValues.put(ContactsContract.CommonDataKinds.Email.ADDRESS, email); c.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, contentValues); } cursor.close(); }
/** * 下载zip文件直接解压 * * @param context * @param dataUrl * @return */ public static boolean downloadAndUnzip(Context context, String dataUrl, String outPath) { InputStream is = null; try { URL url = new URL(dataUrl); // URL url = new URL(urlAddParams(context, dataUrl)); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.addRequestProperty("User-Agent-YN", DeviceUtil.getDeviceInfoForUserAgentYN()); is = conn.getInputStream(); String fileName = getFileNameForUrl(dataUrl); String zipFileName; int index = fileName.indexOf("."); if (index != -1) { zipFileName = fileName.substring(0, index); } else { zipFileName = fileName; } return unZip(is, outPath, zipFileName); } catch (Exception e) { e.printStackTrace(); return false; } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } }
public static Bitmap getOptionBitmapByScaleOfWinWidth( Context context, Uri imgUri, float scaleOfWinWidth) throws FileNotFoundException { if (imgUri == null) return null; // Get the dimensions of the bitmap BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = true; InputStream is = null; try { is = context.getContentResolver().openInputStream(imgUri); BitmapFactory.decodeStream(is, null, bmOptions); } catch (Exception e) { } finally { IOUtils.closeQuietly(is); } int photoW = bmOptions.outWidth; int photoH = bmOptions.outHeight; int tw = (int) (DeviceUtil.getDeviceWidth(context) * scaleOfWinWidth); if (tw == 0) { return null; } int th = (int) (tw * ((double) photoH / photoW)); int targetW = tw; int targetH = th; // Determine how much to scale down the image int scaleFactor = Math.min(photoW / targetW, photoH / targetH); // Decode the image file into a Bitmap sized to fill the View bmOptions.inJustDecodeBounds = false; bmOptions.inSampleSize = scaleFactor; bmOptions.inPurgeable = true; InputStream nis = null; Bitmap bitmap = null; try { nis = context.getContentResolver().openInputStream(imgUri); bitmap = BitmapFactory.decodeStream(nis, null, bmOptions); } catch (Exception e) { } finally { IOUtils.closeQuietly(nis); } return bitmap; }
private static boolean doDownload(Context context, String imageUrl, File outFile) { URL url = null; InputStream is = null; FileOutputStream fos = null; try { url = new URL(imageUrl); // url = new URL(urlAddParams(context, imageUrl)); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.addRequestProperty("User-Agent-YN", DeviceUtil.getDeviceInfoForUserAgentYN()); is = conn.getInputStream(); fos = new FileOutputStream(outFile); byte[] buf = new byte[1024]; int len = 0; while ((len = is.read(buf)) > 0) { fos.write(buf, 0, len); } fos.flush(); conn.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } finally { try { if (is != null) { is.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } return true; }