当前位置:操作系统 > 安卓/Android >>

android 从相机或者相册获取照片!

最近项目用到调用相机,给出代码,只是调用的android api 给的接口 上代码:


1.  ackage com.ray.test; 

2.   

3.  import android.app.Activity; 

4.  import android.app.AlertDialog; 

5.  import android.content.ActivityNotFoundException; 

6.  import android.content.Context; 

7.  import android.content.DialogInte易做图ce; 

8.  import android.content.Intent; 

9.  import android.database.Cursor; 

10. import android.net.Uri; 

11. import android.os.Bundle; 

12. import android.os.Environment; 

13. import android.provider.MediaStore; 

14. import android.util.Log; 

15. import android.view.ContextThemeWrapper; 

16. import android.view.View; 

17. import android.view.View.OnClickListener; 

18. import android.widget.ArrayAdapter; 

19. import android.widget.Button; 

20. import android.widget.ImageView; 

21. import android.widget.ListAdapter; 

22. import android.widget.Toast; 

23.  

24. public class TestPhotoActivity extends Activity implements OnClickListener 

25. { 

26.  

27.     private static final String TAG = "TestPhotoActivity"; 

28.  

29.     /* 用来标识请求照相功能的activity */ 

30.     private static final int CAMERA_WITH_DATA = 3023; 

31.  

32.     /* 用来标识请求gallery的activity */ 

33.     private static final int PHOTO_PICKED_WITH_DATA = 3021; 

34.  

35.     Button buttonOK; 

36.  

37.     ImageView imageViewPhoto; 

38.  

39.     /** Called when the activity is first created. */ 

40.     @Override 

41.     public void onCreate(Bundle savedInstanceState) 

42.     { 

43.         super.onCreate(savedInstanceState); 

44.         setContentView(R.layout.main); 

45.         buttonOK = (Button) findViewById(R.id.ButtonOK); 

46.  

47.         imageViewPhoto = (ImageView) findViewById(R.id.imageViewPhoto); 

48.  

49.         buttonOK.setOnClickListener(this); 

50.  

51.     } 

52.  

53.     @Override 

54.     public void onClick(View v) 

55.     { 

56.         // TODO Auto-generated method stub 

57.         switch (v.getId()) 

58.         { 

59.             case R.id.ButtonOK: 

60.  

61.                 doPickPhotoAction(); 

62.                 break; 

63.  

64.             default: 

65.                 break; 

66.         } 

67.  

68.     } 

69.  

70.     private void doPickPhotoAction() 

71.     { 

72.         Context context = TestPhotoActivity.this; 

73.  

74.         // Wrap our context to inflate list items using correct theme 

75.         final Context dialogContext = new ContextThemeWrapper(context, 

76.             android.R.style.Theme_Light); 

77.         String cancel = "返回"; 

78.         String[] choices; 

79.         choices = new String[2]; 

80.         choices[0] = getString(R.string.take_photo); //拍照 

81.         choices[1] = getString(R.string.pick_photo); //从相册中选择 

82.         final ListAdapter adapter = new ArrayAdapter<String>(dialogContext, 

83.             android.R.layout.易做图_list_item_1, choices); 

84.  

85.         final AlertDialog.Builder builder = new AlertDialog.Builder( 

86.             dialogContext); 

87.         builder.setTitle(R.string.attachToContact); 

88.         builder.setSingleChoiceItems(adapter, -1, 

89.             new DialogInte易做图ce.OnClickListener() 

90.             { 

91.                 public void onClick(DialogInte易做图ce dialog, int which) 

92.                 { 

93.                     dialog.dismiss(); 

94.                     switch (which) 

95.                     { 

96.                         case 0: 

97.                         { 

98.                             String status = Environment 

99.                                 .getExternalStorageState(); 

100.                             if (status.equals(Environmen

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,