当前位置: 代码迷 >> Android >> android调用系统资源剪裁图片
  详细解决方案

android调用系统资源剪裁图片

热度:86   发布时间:2016-05-01 18:42:10.0
android调用系统资源裁剪图片

1 import java.io.File;
2 import android.app.Activity;
3 import android.content.Intent;
4 import android.graphics.drawable.Drawable;
5 import android.net.Uri;
6 import android.os.Bundle;
7 import android.view.View;
8 import android.view.View.OnClickListener;
9 import android.widget.Button;
10
11 public class GalleryActivity extends Activity {
12
13 ??? private static int SELECT_PICTURE;//返回标志位 filed
14
15 ??? private File tempFile;
16
17 ??? Button button;
18
19 ??? /** Called when the activity is first created. */
20 ??? @Override
21 ??? public void onCreate(Bundle savedInstanceState) {
22 ??????? super.onCreate(savedInstanceState);
23 ??????? this.tempFile = new File("/sdcard/a.jpg");// 这句一定要在onCreate()里面调用
24 ??????? button = new Button(this);
25 ??????? button.setText("获取图片");
26 ??????? button.setOnClickListener(new OnClickListener() {
27 ??????????? @Override
28 ??????????? public void onClick(View v) {
29 ??????????????? Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
30 ??????????????? intent.setType("image/*");
31 ??????????????? intent.putExtra("crop", "true");// crop=true 有这句才能出来最后的裁剪页面.
32
33 ??????????????? intent.putExtra("aspectX", 1);// 这两项为裁剪框的比例.
34 ??????????????? intent.putExtra("aspectY", 2);// x:y=1:2
35
36 ??????????????? intent.putExtra("output", Uri.fromFile(tempFile));
37 ??????????????? intent.putExtra("outputFormat", "JPEG");//返回格式
38
39 ??????????????? startActivityForResult(Intent.createChooser(intent, "选择图片"), SELECT_PICTURE);
40 ??????????? }
41 ??????? });
42 ??????? setContentView(button);
43 ??? }
44
45 ??? /**
46 ???? * 裁剪完图片后系统调用的方法:onActivityResult
47 ???? */
48 ??? @Override
49 ??? protected void onActivityResult(int requestCode, int resultCode, Intent data) {
50 ??????? if (resultCode == RESULT_OK)
51 ??????????? if (requestCode == SELECT_PICTURE)
52 ??????????????? button.setBackgroundDrawable(Drawable.createFromPath(tempFile.getAbsolutePath()));
53 ??? }
54 }

?

?

?

  1. protected?void?onActivityResult(int?requestCode,?int?resultCode,?Intent?data)?{ ??
  2. ????????if(resultCode?==?RESULT_OK)?{ ??
  3. ????????????switch(requestCode)?{ ??
  4. ????????????????case?REQ_CODE_CAMERA: ??
  5. ????????????????????uploadImage(photoTemp); ??
  6. ????????????????????break; ??
  7. ????????????????case?REQ_CODE_PICTURES: ??
  8. ????????????????????Uri?uri?=?data.getData(); ??
  9. ????????????????????ContentResolver?cr?=?this.getContentResolver(); ??
  10. ????????????????????//get?the?physical?path?of?the?image ??
  11. ????????????????????Cursor?c?=?cr.query(uri,?null,?null,?null,?null); ??
  12. ????????????????????c.moveToFirst(); ??
  13. ????????????????????photoTemp?=?c.getString(c.getColumnIndex("_data")); ??
  14. ????????????????????uploadImage(photoTemp); ??
  15. ????????????????????break; ??
  16. ????????????????default: ??
  17. ????????????????????break; ??
  18. ????????????}; ??
  19. ????????} ??
  20. ????????super.onActivityResult(requestCode,?resultCode,?data); ??
  21. ????}??

?

?

  相关解决方案