首页 > 其他分享 >手机照相或选择相册,类似新浪微博的图片处理

手机照相或选择相册,类似新浪微博的图片处理

时间:2023-02-28 11:35:14浏览次数:33  
标签:11 相册 putExtra 照相 微博 Intent new intent data


拍照的

1. btn1.setOnClickListener(new OnClickListener() {  
2.
3. @Override
4. public void onClick(View v) {
5.
6. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
7.
8. File out = new File(Environment.getExternalStorageDirectory(),
9. "camera.png");
10. Uri uri = Uri.fromFile(out);
11. intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
12. xxActivity.startActivityForResult(intent, 3);
13. }
14. });

相册

1. btn2.setOnClickListener(new OnClickListener() {  
2.
3. @Override
4. public void onClick(View v) {
5. Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
6. intent.addCategory(Intent.CATEGORY_OPENABLE);
7. intent.setType("image/*");
8. intent.putExtra("crop", "true");
9. intent.putExtra("aspectX", 1);
10. intent.putExtra("aspectY", 1);
11. intent.putExtra("outputX", 80);
12. intent.putExtra("outputY", 80);
13. intent.putExtra("return-data", true);
14.
15. xxActivity.startActivityForResult(intent, 2);
16. }
17. });

处理的activity

Java代码 

1. protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
2. if (resultCode != RESULT_OK) {
3. return;
4. } else {
5.
6. switch (requestCode) {
7. case 2:
8. Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");
9. Home.personalinfo.headbmp = cameraBitmap;
10. try {
11. String str = saveMyBitmap(cameraBitmap);
12.
13.
14. } catch (IOException e) {
15. e.printStackTrace();
16. }
17.
18. break;
19.
20. /* 拍照后保存图片,并跳到裁剪功能 */
21. case 3:
22.
23. new saveAsy().execute("");
24. break;
25. }
26.
27. }
28. super.onActivityResult(requestCode, resultCode, data);
29. }

 saveAsy的方法

Java代码 

1. class saveAsy extends AsyncTask<String, Object, String> {  
2.
3.
4.
5.
6.
7. @Override
8. protected String doInBackground(String... str) {
9. dispathBitmap();
10. Intent intent = new Intent("com.android.camera.action.CROP");
11. try {
12. intent.setData(Uri
13. .parse(android.provider.MediaStore.Images.Media
14. .insertImage(getContentResolver(),
15. storageUrl+"/camera.png", null, null)));
16. } catch (FileNotFoundException e) {
17. e.printStackTrace();
18. }
19. intent.putExtra("crop", "true");
20. intent.putExtra("aspectX", 1);
21. intent.putExtra("aspectY", 1);
22. intent.putExtra("outputX", 80);
23. intent.putExtra("outputY", 80);
24. intent.putExtra("return-data", true);
25. startActivityForResult(intent, 2);
26.
27. return "";
28. }
29.
30. @Override
31. protected void onPostExecute(String bmp) {
32. super.onPostExecute(bmp);
33.
34. }
35.
36. }

标签:11,相册,putExtra,照相,微博,Intent,new,intent,data
From: https://blog.51cto.com/u_15070324/6090620

相关文章