Android gets photos from a camera or album!

Source: Internet
Author: User

Recently, the project used to call the camera and provide the code. It only calls the code on the interface provided by the 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. DialogInterface;

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 used to identify the photo request function */

30. private static final int camera_withh_data = 3023;

31.

32./* used to identify the activity requesting gallery */

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 = "return ";

78. String [] choices;

79. choices = new String [2];

80. choices [0] = getString (R. string. take_photo); // take a photo

81. choices [1] = getString (R. string. pick_photo); // select from album

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

83. android. R. layout. simple_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 DialogInterface. OnClickListener ()

90 .{

91. public void onClick (DialogInterface 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 (Environment. MEDIA_MOUNTED ))

101. {// determine whether an SD card exists

102. doTakePhoto (); // the user clicks to get it from the camera

103 .}

104. else

105 .{

106. Toast. makeText (TestPhotoActivity. this, "No SD card ",

107. Toast. LENGTH_LONG). show ();

108 .}

109. break;

110 .}

111. case 1:

112. doPickPhotoFromGallery (); // obtain from the album

113. break;

114 .}

115 .}

116 .});

117. builder. setNegativeButton (cancel, new DialogInterface. OnClickListener ()

118 .{

119.

120. @ Override

121. public void onClick (DialogInterface dialog, int which)

122 .{

123. dialog. dismiss ();

124 .}

125.

126 .});

127. builder. create (). show ();

128 .}

129.

130 ./**

131. * Take a photo and obtain the image

132 .*

133 .*/

134. protected void doTakePhoto ()

135 .{

136. try

137 .{

138. Intent intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE, null );

139. // used directly without downgrading

140. startActivityForResult (intent, CAMERA_WITH_DATA );

141 .}

142. catch (ActivityNotFoundException e)

143 .{

144. Toast. makeText (this, R. string. photoPickerNotFoundText,

145. Toast. LENGTH_LONG). show ();

146 .}

147 .}

148.

149. // request the Gallery program

150. protected void doPickPhotoFromGallery ()

151 .{

152. try

153 .{

154.

155. final Intent intent = getPhotoPickIntent ();

156.

157. startActivityForResult (intent, PHOTO_PICKED_WITH_DATA );

158.

159 .}

160. catch (ActivityNotFoundException e)

161 .{

162. Toast. makeText (this, R. string. photoPickerNotFoundText1,

163. Toast. LENGTH_LONG). show ();

164 .}

165 .}

166.

167. /// encapsulate the intent of the Gallery request

168. public static Intent getPhotoPickIntent ()

169 .{

170. Intent intent = new Intent (Intent. ACTION_GET_CONTENT, null );

171. intent. setType ("image /*");

172.

173. return intent;

174 .}

175.

176. // because Camera and Gally are called, it is necessary to judge their respective responses. This is the startActivityForResult when they are started.

177. protected void onActivityResult (int requestCode, int resultCode, Intent data)

178 .{

179. if (resultCode! = RESULT_ OK)

180. return;

181. switch (requestCode)

182 .{

183. case PHOTO_PICKED_WITH_DATA:

184.

185. Uri uri = data. getData ();

186.

187. String selectedImagePath = getPath (uri );

188.

189. Log. e (TAG, "1111111111" + selectedImagePath );

190 .//

191. imageViewPhoto. setImageURI (uri );

192.

193. break;

194.

195. case CAMERA_WITH_DATA:

196.

197. Uri uri1 = data. getData ();

198.

199. Log. e (TAG, uri1.toString ());

200 .//

201. String selectedImagePath1 = getPath (uri1 );

202. Log. e (TAG, "1111111111" + selectedImagePath1 );

203. imageViewPhoto. setImageURI (uri1 );

204.

205. break;

206.

207 .}

208 .}

209.

210. public String getPath (Uri uri)

211 .{

212. String [] projection = {MediaStore. Images. Media. DATA };

213. Cursor cursor = managedQuery (uri, projection, null );

214. int column_index = cursor

215. getColumnIndexOrThrow (MediaStore. Images. Media. DATA );

216. cursor. moveToFirst ();

217. return cursor. getString (column_index );

218 .}

219 .}

From chenghai2011

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.