android 关于sdcard assets drawable bitmap 之间处理问题

/**
  * 从sdcard获取图片转换为bitmap或drawable
  * @param path
  * @return
  */
 @SuppressWarnings("deprecation")
 public static Drawable stringToDrawable(String path){
  try {
   FileInputStream input = new FileInputStream(new File(path));
   Bitmap bmp = BitmapFactory.decodeStream(input);
   input.close();
   Drawable drawable = new BitmapDrawable(bmp);
   return drawable;
  } catch (IOException e) {
   e.printStackTrace();
  }
  return null;
 }
 
 /**
  * 从assets文件夹获取图片转换为bitmap或drawable
  * @param ctx
  * @param fileName
  * @return
  */

String path = "images/" +  "a/" +  "b.png";
    @SuppressWarnings("deprecation")
 public static Drawable getImageFromAssetsFile(Context ctx,String fileName){
        AssetManager am = ctx.getResources().getAssets();
        try{
            InputStream input = am.open(fileName);
            Bitmap bmp = BitmapFactory.decodeStream(input);
            input.close();
            Drawable drawable = new BitmapDrawable(bmp);
            return drawable;
        }catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }

 

/**
  * 保存图片到sdcard,不做压缩
  * @param bitmap
  * @return
  */
 public static String storeInSD(Bitmap bitmap) {
  String path = Environment.getExternalStorageDirectory() + File.separator + "Test";
  File file = new File(path);
  if (!file.exists()) {
   file.mkdir();
  }
  File imageFile = new File(file, System.currentTimeMillis() + ".jpg");
  try {
   imageFile.createNewFile();
   FileOutputStream fos = new FileOutputStream(imageFile);
   bitmap.compress(CompressFormat.JPEG, 100, fos);
   fos.flush();
   fos.close();
   return imageFile.getAbsolutePath();
  } catch (IOException e) {
   e.printStackTrace();
   return null;
  }
 }

 

/**
  * 保存string到test.xml到sdcard
  * @param context
  * @param fileName "test.xml"    /data/data/com.test.activity/files/test.xml
  * @param txt
  * @return
  */
 public static boolean storeInSD(Context context,String fileName,String txt) {
  try {
   FileOutputStream os = context.openFileOutput(fileName,Context.MODE_PRIVATE);
   os.write(txt.getBytes("UTF-8"));  
   os.close();
   return true;
  } catch (IOException e) {
   e.printStackTrace();
   return false;
  }
 }

 

/**
  * 从文件夹读xml文件
  * @param path  "/data/data/com.test.activity/files/test.xml"
  * @return
  */
 public static ArrayList<MyTrackPoints> readSdXML(String path){
  File file = new File(path);
  try {
   if(!file.exists()){
    file.createNewFile();
   }
   InputStream input = new FileInputStream(file);
   ArrayList<MyTrackPoints> list = MyXml.parserXml(input);
   return list;
  } catch (IOException e) {
   e.printStackTrace();
   return null;
  }
 }

 

/**
  * 判断sdcard是否存在
  * @return
  */
 public static boolean isHasSdcard() {
  String status = Environment.getExternalStorageState();
  if (status.equals(Environment.MEDIA_MOUNTED)) {
   return true;
  } else {
   return false;
  }
 }

 /**
  * 判断文件是否存在
  * @param dir
  * @return
  */
 public static boolean isFileExists(String dir) {
  File destDir = new File(dir);
  if (!destDir.exists()) {
   destDir.mkdirs();
   return true;
  } else {
   return false;
  }
 }

/**获取当前格式化时间
  * get cur time
  */
 @SuppressLint("SimpleDateFormat")
 public static String getCurTime() {
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");      
  Date curDate = new Date(System.currentTimeMillis());
  return formatter.format(curDate);
 }
 
 /**删除文件
  * delete file
  * @param filePath
  * @return
  */
 public boolean deleteFile(String filePath) {
     File file = new File(filePath);
        if (file.isFile() && file.exists()) {
         return file.delete();
        }
        return false;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值