服务器之家

服务器之家 > 正文

android编程实现系统图片剪裁的方法

时间:2021-04-09 14:13     来源/作者:年轻的zhangchang

本文实例讲述了android编程实现系统图片剪裁的方法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package cn.test;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class CutActivity extends Activity {
 private Button button;
 private ImageView imageView;
 private File mCurrentPhotoFile;
 private Bitmap cameraBitmap;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  button = (Button) findViewById(R.id.button);
  imageView = (ImageView) findViewById(R.id.imageView);
  button.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    Intent intent = new Intent(
    "android.media.action.IMAGE_CAPTURE");
    mCurrentPhotoFile = new File(
      "mnt/sdcard/DCIM/Camera/",
      getPhotoFileName());
   intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCurrentPhotoFile));
  startActivityForResult(
    intent,
    Activity.DEFAULT_KEYS_DIALER);
   }
  });
 }
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  switch (requestCode) {
  case 1:
   Uri imgUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
   ContentResolver cr = CutActivity.this
     .getContentResolver();
   Uri fileUri = Uri.fromFile(mCurrentPhotoFile);
   sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
     fileUri));
   try {
    Thread.sleep(3000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   Cursor cursor = cr
     .query(imgUri, null,
       MediaStore.Images.Media.DISPLAY_NAME + "='"
         + mCurrentPhotoFile.getName() + "'",
       null, null);
   Uri uri = null;
   if (cursor != null && cursor.getCount() > 0) {
    cursor.moveToLast();
    long id = cursor.getLong(0);
    uri = ContentUris.withAppendedId(imgUri, id);
   }
   final Intent intent = new Intent(
     "com.android.camera.action.CROP");
   intent.setDataAndType(uri, "image/*");
   intent.putExtra("crop", "true");
   intent.putExtra("outputX", 380);
   intent.putExtra("outputY", 500);
   intent.putExtra("return-data", true);
   CutActivity.this.startActivityForResult(intent, 3);
   break;
  case 2:
   break;
  case 3:
   if (data != null) {
    cameraBitmap = (Bitmap) data.getExtras().get("data");
    imageView.setImageBitmap(cameraBitmap);
   }
   break;
  default:
   break;
  }
  super.onActivityResult(requestCode, resultCode, data);
 }
 private String getPhotoFileName() {
  Date date = new Date(System.currentTimeMillis());
  SimpleDateFormat dateFormat = new SimpleDateFormat(
    "'IMG'_yyyyMMdd_HHmmss");
  return dateFormat.format(date) + ".jpg";
 }
}

希望本文所述对大家Android程序设计有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部