当前位置:   article > 正文

android读写图片文件路径,Android拷贝图片到指定文件路径

安卓里怎么用java写图片路径



1.拷贝图片的工具类

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.Environment;

import android.text.TextUtils;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

/**

* Created by Administrator  .

*/

public class BitmapUtil {

private static final String DEAFAULT_FILE_PATH = getSdCardPath() + File.separator;

private BitmapUtil() {

}

/**

* 以最省内存的方式读取本地资源的图片

*

* @param context

* @param resId

* @return

*/

public static Bitmap readBitMap(Context context, int resId) {

BitmapFactory.Options opt = new BitmapFactory.Options();

opt.inPreferredConfig = Bitmap.Config.RGB_565;

opt.inPurgeable = true;

opt.inInputShareable = true;

// 获取资源图片

InputStream is = context.getResources().openRawResource(resId);

return BitmapFactory.decodeStream(is, null, opt);

}

Bitmap bmp = BitmapUtil.getDiskBitmap(path);

if(bmp != null) {

mHeaderIv.setImageBitmap(bmp);

}

/**

* 读取本地图片

* @param path 图片路径

* */

public static Bitmap getDiskBitmap(String path) {

Bitmap bitmap = null;

if(TextUtils.isEmpty(path)) {

return bitmap;

}

try {

File file = new File(path);

if (file.exists()) {

BitmapFactory.Options opt = new BitmapFactory.Options();

opt.inPreferredConfig = Bitmap.Config.RGB_565;

opt.inPurgeable = true;

opt.inInputShareable = true;

bitmap = BitmapFactory.decodeFile(path, opt);

}

} catch (Exception e) {

}

return bitmap;

}

/**

* 保存图片到本地 第一个参数是图片名称 第二个参数为需要保存的bitmap

* */

public static void saveImgToDisk(String imgName, Bitmap bitmap) {

File file = new File(DEAFAULT_FILE_PATH, imgName);

if(file == null) {

return;

}

if(isFileExists(file.getPath())) {

return;

}

try {

FileOutputStream out = new FileOutputStream(file);

bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);

String path = file.getPath();

out.flush();

out.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 判断文件路径是否已经存在

* @param filePath 文件路径

* */

private static boolean isFileExists(String filePath) {

try {

File file = new File( filePath );

return file.exists();

} catch (Exception e) {

e.printStackTrace();

}

return false;

}

/**

* 返回SD卡跟目录

*

* @return SD卡跟目录

*/

public static String getSdCardPath() {

File sdDir ;

boolean sdCardExist = isSdCardExist(); // 判断sd卡是否存在

if (sdCardExist) {

sdDir = Environment.getExternalStorageDirectory();// 获取跟目录

return sdDir.toString();

} else {

return null;

}

}

/**

* 判断SD卡是否存在

*

* @return SD卡存在返回true,否则返回false

*/

public static boolean isSdCardExist() {

return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);

}

}

2.读取本地图片

Bitmap bmp = BitmapUtil.getDiskBitmap(path);

if(bmp != null) {

mHeaderIv.setImageBitmap(bmp);

}

3.保存图片到本地  Bitmap bmp = BitmapFactory.decodeFile(localPath);  saveImgToDisk(fileName, bmp);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/381154
推荐阅读
相关标签
  

闽ICP备14008679号