溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

拍照剪切and獲取sd卡中的圖片

發(fā)布時間:2020-07-25 00:47:59 來源:網(wǎng)絡(luò) 閱讀:364 作者:android紫函 欄目:移動開發(fā)

package com.truekey.alarmclock.user;

import java.io.File;

import android.app.Activity;

import android.app.Dialog;

import android.content.Context;

import android.content.Intent;

import android.content.SharedPreferences;

import android.graphics.Bitmap;

import android.graphics.drawable.BitmapDrawable;

import android.graphics.drawable.Drawable;

import android.net.Uri;

import android.os.Bundle;

import android.os.Environment;

import android.provider.MediaStore;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.Window;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.RelativeLayout.LayoutParams;

import android.widget.TextView;

import com.truekey.alarmclock.R;


public class UserInfoActivity extends Activity {

ImageView user_header_p_w_picpath;

Dialog pictureDialog;

View pictureView;

Button takePictureBtn, allPicture, cancle;

private static final int SCALE = 15;// 照片縮小比例

String imgBase64;

String fileName = null;

ImageView iv_title_left, iv_title_right;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.user_info_layout);

iv_title_right = (ImageView) findViewById(R.id.iv_title_right);

iv_title_left = (ImageView) findViewById(R.id.iv_title_left);

user_header_p_w_picpath = (ImageView) findViewById(R.id.user_header_p_w_picpath);

TextView nicheng_tv = (TextView) findViewById(R.id.nicheng_tv);

pictureDialog();

}


public void onClick(View v) {

switch (v.getId()) {

case R.id.user_header_p_w_picpath:

pictureDialog.show();

pictureDialog.getWindow().setLayout(LayoutParams.MATCH_PARENT,

LayoutParams.WRAP_CONTENT);

break;

case R.id.iv_title_right:

finish();

overridePendingTransition(R.anim.home_main_last_twoactivity_out,

R.anim.ptr_slide_out_to_bottom);

break;

case R.id.iv_title_left:

finish();

overridePendingTransition(R.anim.home_main_last_twoactivity_in,

R.anim.ptr_slide_out_to_bottom);

default:

break;

}

}

// 拍照布局

public void pictureDialog() {

pictureView = LayoutInflater.from(UserInfoActivity.this).inflate(

R.layout.takepicture_dialog, null);

cancle = (Button) pictureView.findViewById(R.id.cancle);

takePictureBtn = (Button) pictureView.findViewById(R.id.take_picture);

takePictureBtn.setOnClickListener(new ButtonOnClickListener());

// 所有的圖片選取。。

allPicture = (Button) pictureView.findViewById(R.id.all_picture);

// 所有圖片

allPicture.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new Intent(Intent.ACTION_PICK, null);

intent.setDataAndType(

MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "p_w_picpath/*");

startActivityForResult(intent, 1);

}

});


// 取消

cancle.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

pictureDialog.dismiss();

}

});

if (pictureView != null) {

/* 自定義對話框 */

pictureDialog = new Dialog(this, R.style.mydialog);

pictureDialog.setContentView(pictureView);

pictureDialog.setCancelable(false);

Window window = pictureDialog.getWindow();

window.setWindowAnimations(R.style.AlterDialogAnima);

window.setGravity(Gravity.BOTTOM);

}

}

public final static int CAMERA_RESULT = 8888;

// 拍照獲取圖片

private class ButtonOnClickListener implements View.OnClickListener {

@Override

public void onClick(View v) {

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

// 下面這句指定調(diào)用相機(jī)拍照后的照片存儲的路徑

intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(

Environment.getExternalStorageDirectory(), "xiaoma.jpg")));

startActivityForResult(intent, 2);

}

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK) {

switch (requestCode) {

case 1:

startPhotoZoom(data.getData());

break;

// 如果是調(diào)用相機(jī)拍照時

case 2:

File temp = new File(Environment.getExternalStorageDirectory()

+ "/xiaoma.jpg");

startPhotoZoom(Uri.fromFile(temp));

break;

// 取得裁剪后的圖片

case 3:

/**

* 非空判斷大家一定要驗(yàn)證,如果不驗(yàn)證的話, 在剪裁之后如果發(fā)現(xiàn)不滿意,要重新裁剪,丟棄

* 當(dāng)前功能時,會報NullException,小馬只 在這個地方加下,大家可以根據(jù)不同情況在合適的 地方做判斷處理類似情況

*

*/

if (data != null) {

setPicToView(data);

}

break;

default:

break;

}

}

}

/**

* 裁剪圖片方法實(shí)現(xiàn)

*

* @param uri

*/

public void startPhotoZoom(Uri uri) {

Intent intent = new Intent("com.android.camera.action.CROP");

intent.setDataAndType(uri, "p_w_picpath/*");

// 下面這個crop=true是設(shè)置在開啟的Intent中設(shè)置顯示的VIEW可裁剪

intent.putExtra("crop", "true");

// aspectX aspectY 是寬高的比例

intent.putExtra("aspectX", 1);

intent.putExtra("aspectY", 1);

// outputX outputY 是裁剪圖片寬高

intent.putExtra("outputX", 150);

intent.putExtra("outputY", 150);

intent.putExtra("return-data", true);

startActivityForResult(intent, 3);

}

/**

* 保存裁剪之后的圖片數(shù)據(jù)

*

* @param picdata

*/

private void setPicToView(Intent picdata) {

Bundle extras = picdata.getExtras();

if (extras != null) {

Bitmap photo = extras.getParcelable("data");

Drawable drawable = new BitmapDrawable(photo);

pictureDialog.dismiss();

// 為防止原始圖片過大導(dǎo)致內(nèi)存溢出,這里先縮小原圖顯示,然后釋放原始Bitmap占用的內(nèi)存

Bitmap choosepicture = ImageTools.toRoundBitmap2(photo);

// 釋放原始圖片占用的內(nèi)存,防止out of memory異常發(fā)生

photo.recycle();

user_header_p_w_picpath.setImageBitmap(choosepicture);

}

}


// 用于保存圖片

public void putSharedpreference(String takepicture) {

SharedPreferences shared = getSharedPreferences("temp",

Context.MODE_PRIVATE);

SharedPreferences.Editor edit = shared.edit();

edit.putString("picture", takepicture);

edit.commit();

}


public String getSharedpreference() {

SharedPreferences shared = getSharedPreferences("temp",

Context.MODE_PRIVATE);

return shared.getString("picture", "");

}


@Override

protected void onDestroy() {

super.onDestroy();

putSharedpreference(fileName);

}


@Override

protected void onResume() {

super.onResume();


}

}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI