引用:http://www.oschina.net/code/snippet_4873_4437
//1,已将图片保存到drawable目录下 |
02 |
03 |
//通过图片id获得Drawable |
04 |
05 |
Resource res=gerResource(); |
06 |
Drawable drawable=res.getDrawable(id); //id为R.drawable.图片名称 |
07 |
08 |
//通过图片id获得Bitmap |
09 |
10 |
Resource res=gerResource(); |
11 |
12 |
Bitmap bitmap=BitmapFactory.decodeResource(res, id); |
13 |
14 |
//若只知道图片的名称,可以通过图片的名称获得图片的id |
15 |
16 |
//name:图片的名称 defType:图片的类型(png,jpeg),defPackage:工程的包名 |
17 |
int id = int id =res.getIdentifier(name, defType, defPackage); |
18 |
//获得id之后可以根据你的需要来获得Bitmap或Drawable |
19 |
20 |
//2,已将图片保存到assest目录下 |
21 |
22 |
//知道图片的名称,通过inputstream打开图片 |
23 |
24 |
AssetManager asm=getAssetMg(); |
25 |
InputStream is=asm.open(name); //name:图片的名称 |
26 |
//获得Drawable |
27 |
Drawable da = Drawable.createFromStream(is, null ); |
28 |
//获得Bitmap |
29 |
Bitmap bitmap=BitmapFactory.decodeStream(is); |
30 |
31 |
//3,图片保存在sdcard,已知图片的路径 |
32 |
33 |
//图片路径 |
34 |
String path = Environment.getExternalStorageDirectory().toString()+ "/DCIM/device.png" ; |
35 |
RandomAccessFile mMiniThumbFile; |
36 |
File imgfile = new File(path); |
37 |
try { |
38 |
mMiniThumbFile = new RandomAccessFile(imgfile, "rw" ); |
39 |
} catch (IOException ex) { |
40 |
// Open as read-only so we can at least read the existing |
41 |
// thumbnails. |
42 |
try { |
43 |
mMiniThumbFile = new RandomAccessFile(imgfile, "r" ); |
44 |
} catch (IOException ex2) { |
45 |
// ignore exception |
46 |
System.out.println(ex2.toString()); |
47 |
} |
48 |
} |
49 |
data= new byte [ 10553 ]; |
50 |
try { |
51 |
mMiniThumbFile.seek( 0 ); |
52 |
int got = mMiniThumbFile.read(data, 0 , 10552 ); |
53 |
System.out.println( "got=" +got); |
54 |
} catch (IOException e) { |
55 |
// TODO Auto-generated catch block |
56 |
e.printStackTrace(); |
57 |
System.out.println(e.toString()); |
58 |
} |
59 |
if (data != null ) { |
60 |
//通过data获得bitmap |
61 |
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0 ,data.length); |