- /*
- * Copyright (c) 2013-2014, thinkjoy Inc. All Rights Reserved.
- *
- * Project Name: Jiaxiao
- */
- package com.jlusoft.banbantong.common;
- import java.io.InputStream;
- import java.lang.reflect.Field;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.content.pm.PackageManager.NameNotFoundException;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.drawable.BitmapDrawable;
- import android.graphics.drawable.Drawable;
- import android.net.Uri;
- import android.text.TextUtils;
- import android.util.Log;
- import com.jlusoft.banbantong.R;
- import com.jlusoft.banbantong.R.string;
- /**
- * 资源相关工具类
- * <p/>
- * 创建时间: 2014-8-19 下午5:06:12 <br/>
- *
- * @author xszhang
- * @version
- * @since v0.0.1
- */
- public class AssetsUitls {
- private static String TAG = "AssetsUitls";
- /**
- *
- * 通过包名打开另外一个应用程序
- *
- * @author xszhang
- * @param context
- * @param packageName
- * @param targetActivity
- * @since v0.0.1
- */
- public static void startAnotherApp(Context context, String packageName, String targetActivity) {
- // 判断后的逻辑:
- // 已安装,打开程序,需传入参数包名:"com.xxx.xxx"
- if (checkPackageNameExists(context, packageName)) {
- if (TextUtils.isEmpty(targetActivity)) {
- PackageManager packageManager = context.getPackageManager();
- Intent intent = new Intent();
- intent = packageManager.getLaunchIntentForPackage(packageName);
- context.startActivity(intent);
- } else {
- Intent intent = new Intent();
- ComponentName cn = new ComponentName(packageName, targetActivity);
- intent.setComponent(cn);
- ((Activity) context).startActivityForResult(intent, 1000);
- }
- } else {// 未安装,跳转至market下载该程序
- Uri uri = Uri.parse("market://details?id=" + packageName);// id为包名
- Intent it = new Intent(Intent.ACTION_VIEW, uri);
- context.startActivity(it);
- }
- }
- /**
- * 判断包名是否存在
- *
- * @param context
- * @param packageName
- * 完整的包名(eg.com.xxx.xx)
- * @return 返回true存在,返回false不存在
- */
- public static boolean checkPackageNameExists(Context context, String packageName) {
- if (packageName == null || "".equals(packageName))
- return false;
- try {
- context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
- return true;
- } catch (NameNotFoundException e) {
- return false;
- }
- }
- /**
- * 判断类名是否存在
- *
- * @param ClassNameFull
- * 完整的类名(eg.com.xxx.xx)
- * @return 返回true存在,返回false不存在
- */
- public static boolean checkClassNameExists(String className) {
- if (className == null || "".equals(className))
- return false;
- try {
- Class.forName(className);
- return true;
- } catch (ClassNotFoundException e) {
- return false;
- }
- }
- /**
- * 以最省内存的方式读取本地资源的图片
- *
- * @param context
- * @param fileName
- * 资源文件名
- * @return
- */
- public static Bitmap getBitmapFromAssetsFileName(Context context, String fileName) {
- try {
- return getBitmapFromStream(getStreamFromAssetsFileName(context, fileName), 1);
- } catch (Exception e) {
- e.printStackTrace();
- Log.e(TAG, "getBitmapFromAssetsFileName is Error! ErrorCode = " + e.getMessage());
- }
- return null;
- }
- /**
- * 以最省内存的方式读取本地资源的图片
- *
- * @param context
- * @param resName
- * 资源文件名
- * @return
- */
- public static Bitmap getBitmapFromResFileName(Context context, String resName) {
- try {
- return getBitmapFromResFileID(context, getResIDFromResName(context, "drawable", resName));
- } catch (Exception e) {
- e.printStackTrace();
- Log.e(TAG, "getBitmapFromResFileName is Error! ErrorCode = " + e.getMessage());
- }
- return null;
- }
- /**
- * 通过string资源中的name获取vlaue
- *
- * @param context
- * @param resName
- * 资源文件名
- * @return
- */
- public static String getStringFromResName1(Context context, String resName) {
- try {
- return context.getResources().getString(getResIDFromResName(context, "string", resName));
- } catch (Exception e) {
- e.printStackTrace();
- Log.e(TAG, "getStringFromResName is Error! ErrorCode = " + e.getMessage());
- }
- return "";
- }
- /**
- * 通过string资源中的name获取vlaue
- *
- * @param context
- * @param resName
- * 资源文件名
- * @return
- */
- public static String getStringFromResName2(Context context, String resName) {
- Class<string> c = R.string.class;
- Field field;
- int value = 0;
- try {
- field = c.getDeclaredField(resName);
- value = field.getInt(null);
- return context.getResources().getString(value);
- } catch (SecurityException e1) {
- e1.printStackTrace();
- } catch (NoSuchFieldException e1) {
- e1.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- }
- return "";
- }
- /**
- * 以最省内存的方式读取本地资源的图片
- *
- * @param context
- * @param resId
- * 资源文件Id
- * @return
- */
- public static Bitmap getBitmapFromResFileID(Context context, int resId) {
- try {
- return getBitmapFromStream(getStreamFromResID(context, resId), 1);
- } catch (Exception e) {
- e.printStackTrace();
- Log.e(TAG, "getBitmapFromResFileID is Error! ErrorCode = " + e.getMessage());
- }
- return null;
- }
- /**
- * 以最省内存的方式读取本地资源的图片
- *
- * @param context
- * @param fileName
- * 资源文件名
- * @return
- */
- public static Drawable getDrawableFromAssetsFileName(Context context, String fileName) {
- try {
- BitmapDrawable drawable = null;
- InputStream stream = getStreamFromAssetsFileName(context, fileName);
- drawable = new BitmapDrawable(stream);
- stream.close();
- return drawable;
- } catch (Exception e) {
- e.printStackTrace();
- Log.e(TAG, "getBitmapFromAssetsFileName is Error! ErrorCode = " + e.getMessage());
- }
- return null;
- }
- /**
- * 以最省内存的方式读取本地资源的图片
- *
- * @param context
- * @param resId
- * 资源文件Id
- * @return
- */
- public static Drawable getDrawableFromResFileID(Context context, int resId) {
- try {
- BitmapDrawable drawable = null;
- InputStream stream = getStreamFromResID(context, resId);
- drawable = new BitmapDrawable(stream);
- stream.close();
- return drawable;
- } catch (Exception e) {
- e.printStackTrace();
- Log.e(TAG, "getBitmapFromResFileID is Error! ErrorCode = " + e.getMessage());
- }
- return null;
- }
- /**
- * 以最省内存的方式读取本地资源的图片
- *
- * @param context
- * @param fileName
- * 资源文件名
- * @return
- */
- public static Drawable getDrawableFromResFileName(Context context, String fileName) {
- try {
- return getDrawableFromResFileID(context, getResIDFromResName(context, "drawable", fileName));
- } catch (Exception e) {
- e.printStackTrace();
- Log.e(TAG, "getBitmapFromResFileName is Error! ErrorCode = " + e.getMessage());
- }
- return null;
- }
- /**
- * 通过资源文件名称、获取源文件ID
- *
- * @param resFolder
- * 资源文件夹 drawable、raw、string
- * @param fileName
- * @return
- */
- public static int getResIDFromResName(Context context, String resFolder, String fileName) {
- /** 通过资源名称找到资源图片 */
- int resID = -1;
- try {
- fileName = fileName.replace(".png", "");
- fileName = fileName.replace(".jpg", "");
- fileName = fileName.replace(".jpeg", "");
- fileName = fileName.replace(".gif", "");
- if (resFolder.trim().length() > 0) {
- resID = context.getResources().getIdentifier(fileName, resFolder, context.getPackageName());
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return resID;
- }
- /**
- * 以下为从Raw文件中读取:
- *
- * @return
- */
- public static InputStream getStreamFromResID(Context context, int resId) {
- try {
- InputStream stream = context.getResources().openRawResource(resId);
- return stream;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- *
- * 以下为直接从assets读取
- *
- * @author xszhang
- * @param context
- * @param fileName
- * @return
- * @since v0.0.1
- */
- public static InputStream getStreamFromAssetsFileName(Context context, String fileName) {
- try {
- InputStream stream = context.getResources().getAssets().open(fileName);
- return stream;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * 通过二进制流,返回缩放后的Bitmap
- *
- * @param stream
- * @param intSampleSize
- * 给定的缩放比例(缩小到原来的1/n)
- * @return
- */
- public static Bitmap getBitmapFromStream(InputStream stream, int intSampleSize) {
- try {
- BitmapFactory.Options m_Options = new BitmapFactory.Options();
- m_Options.inSampleSize = intSampleSize < 1 ? 1 : intSampleSize;
- m_Options.inPurgeable = true;
- Bitmap m_ReturnBitmap = BitmapFactory.decodeStream(stream, null, m_Options);
- stream.close();
- return m_ReturnBitmap;
- } catch (Exception e) {
- e.printStackTrace();
- Log.e(TAG, "getBitmapFromStream is Error !");
- }
- return null;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/1611201514002.html
来源: http://www.codesnippet.cn/detail/1611201514002.html