Android 读取不同位置(drawable,asset,SDCard)的图片资源
2013-01-08 19:56
633 查看
方式一:
已将图片保存到drawable目录下,通过图片id获得Drawable或者Bitmap,此方式最常用。(若只知道图片的名称,还可以通过图片的名称获得图片的id)
(1)通过图片id获得Drawable
Drawable drawable=getResource().getDrawable(R.drawable.xxx);
(2)通过图片id获得Bitmap
Resource res=gerResource();
Bitmap bitmap=BitmapFactory.decodeResource(res, id);
(3)通过图片的名称获得图片的id(两种方法)
方法1: int id =res.getIdentifier(name, defType, defPackage); //name:图片的名,defType:资源类型(drawable,string。。。),defPackage:工程的包名
方法2:
// 用反射机制来获取资源中的图片ID和尺
Field[] fields = R.drawable.class.getDeclaredFields()
for (Field field : fields) {
if (!”icon”.equals(field.getName()))// 除了icon之外的图片
{
int index = 0;
try {
index = field.getInt(R.drawable.class);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 保存图片ID
imgList.add(index);
}
}
获得id之后可以根据你的需要来获得Bitmap或Drawable
方式二:
已将图片保存到assest目录下,知道图片的名称,通过inputstream获得图片Drawabl
或者 Bitmap
AssetManager asm=getAssetMg();
InputStream is=asm.open(name);//name:图片的名称
(1)获得Drawable
Drawable da = Drawable.createFromStream(is, null);
(2)获得Bitmap
Bitmap bitmap=BitmapFactory.decodeStream(is);
方式三:图片保存在sdcard,通过图片的路径h
/图片路径
String imgFilePath = Environment.getExternalStorageDirectory().toString()
+ “/DCIM/device.png”;
(1)文件输入流
public Bitmap getBitmapFromSd(String imgFilePath) {
FileInputStream fis = null;
try {
fis = new FileInputStream(new File(imgFilePath));//文件输入流
Bitmap bmp = BitmapFactory.decodeStream(fis);
Log.d(“sss”, bmp + “”);
return bmp;
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
try {
if(fis!=null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
(2)随机读写文件流
RandomAccessFile raf;
File imgfile = new File(path);
try {
raf = new RandomAccessFile(imgfile, “r”);//以读的方式读取文件流
}
catch (IOException ex) {
e.printStackTrace();
} finally{
try {
if(fis!=null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
data= new byte[1024];
try {
mMiniThumbFile.seek(0);
int got = mMiniThumbFile.read(data, 0, 1024);
} catch (IOException e) {
e.printStackTrace();
}
if (data != null) {
//通过data获得bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
建议使用随机读写文件流,可以防止读取的文件流过大而导致内存溢出
已将图片保存到drawable目录下,通过图片id获得Drawable或者Bitmap,此方式最常用。(若只知道图片的名称,还可以通过图片的名称获得图片的id)
(1)通过图片id获得Drawable
Drawable drawable=getResource().getDrawable(R.drawable.xxx);
(2)通过图片id获得Bitmap
Resource res=gerResource();
Bitmap bitmap=BitmapFactory.decodeResource(res, id);
(3)通过图片的名称获得图片的id(两种方法)
方法1: int id =res.getIdentifier(name, defType, defPackage); //name:图片的名,defType:资源类型(drawable,string。。。),defPackage:工程的包名
方法2:
// 用反射机制来获取资源中的图片ID和尺
Field[] fields = R.drawable.class.getDeclaredFields()
for (Field field : fields) {
if (!”icon”.equals(field.getName()))// 除了icon之外的图片
{
int index = 0;
try {
index = field.getInt(R.drawable.class);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 保存图片ID
imgList.add(index);
}
}
获得id之后可以根据你的需要来获得Bitmap或Drawable
方式二:
已将图片保存到assest目录下,知道图片的名称,通过inputstream获得图片Drawabl
或者 Bitmap
AssetManager asm=getAssetMg();
InputStream is=asm.open(name);//name:图片的名称
(1)获得Drawable
Drawable da = Drawable.createFromStream(is, null);
(2)获得Bitmap
Bitmap bitmap=BitmapFactory.decodeStream(is);
方式三:图片保存在sdcard,通过图片的路径h
/图片路径
String imgFilePath = Environment.getExternalStorageDirectory().toString()
+ “/DCIM/device.png”;
(1)文件输入流
public Bitmap getBitmapFromSd(String imgFilePath) {
FileInputStream fis = null;
try {
fis = new FileInputStream(new File(imgFilePath));//文件输入流
Bitmap bmp = BitmapFactory.decodeStream(fis);
Log.d(“sss”, bmp + “”);
return bmp;
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
try {
if(fis!=null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
(2)随机读写文件流
RandomAccessFile raf;
File imgfile = new File(path);
try {
raf = new RandomAccessFile(imgfile, “r”);//以读的方式读取文件流
}
catch (IOException ex) {
e.printStackTrace();
} finally{
try {
if(fis!=null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
data= new byte[1024];
try {
mMiniThumbFile.seek(0);
int got = mMiniThumbFile.read(data, 0, 1024);
} catch (IOException e) {
e.printStackTrace();
}
if (data != null) {
//通过data获得bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
建议使用随机读写文件流,可以防止读取的文件流过大而导致内存溢出
相关文章推荐
- 关于Android读取不同位置(drawable,asset,SDCard)的图片资源的总结
- 关于Android读取不同位置(drawable,asset,SDCard)的图片资源的总结
- 关于Android读取不同位置(drawable,asset,SDCard)的图片资源的总结
- 关于Android读取不同位置(drawable,asset,SDCard)的图片资源的总结(转)
- 关于Android读取不同位置(drawable,asset,SDCard)的图片资源的总结
- Android读取不同位置(drawable,asset,SDCard)的图片资源
- 关于Android读取不同位置(drawable,asset,SDCard)的图片资源的总结
- 关于Android读取不同位置(drawable,asset,SDCard)的图片资源的总结
- 关于Android读取不同位置(drawable,asset,SDCard)的图片资源的总结
- 关于Android读取不同位置(drawable,asset,SDCard)的图片资源的总结
- Android读取不同位置(drawable,asset,SDCard)下的图片 方法总结
- 【Android】读取sdcard卡上的所有图片并且显示,读取的过程有进度条显示
- Android 读取sdcard上的图片实例(必看)
- android开发之简单图片浏览器 读取sdcard图片+形成缩略图+Gallery
- android-读取Assets图片资源保存到SD - 随心
- android 从sdcard 读取图片并进行剪切,粘贴操作
- 【Android】读取sdcard上的图片
- android-读取Assets图片资源保存到SD - 随心
- Android实现将Drawable资源ID存储到Sdcard上
- 为不同的分辨率提供不同的drawable资源,巧用Android图片资源,打造更精致的APP