package com.gst.thinkpad.helloworld;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
public class TakePhotoActivity extends Activity {
ImageView imageView; //显示图片
File tempFullPath;//文件的目录
Dialog dialog; //提示框
EditText editText; //提示框中的输入框
TextView textViewTitle; //标题
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==1){
if (resultCode==RESULT_OK){
String sdState=Environment.getExternalStorageState();
if (sdState.equals(Environment.MEDIA_MOUNTED)){
String path= tempFullPath.getAbsolutePath();
Toast.makeText(TakePhotoActivity.this,path, Toast.LENGTH_LONG).show();
//可能会内存溢出
Bitmap bitmap= BitmapFactory.decodeFile(path);
imageView.setImageBitmap(bitmap);
//优化方法:http://www.jb51.net/article/34761.htm
//1.对图片本身进行操作
// InputStream inputStream=this.getResources().openRawResource(R.mipmap.b);
Bitmap bitmap2;
InputStream inputStream=null;
try{
BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeFile(tempFullPath.getAbsolutePath(),options);
int realWidth=options.outWidth;
options.inJustDecodeBounds=false;
options.inPreferredConfig= Bitmap.Config.RGB_565;
int thumbWidth=300;
if (thumbWidth>0){
options.inSampleSize=realWidth/thumbWidth;
}
inputStream=new FileInputStream(new File(tempFullPath.getAbsolutePath()));
bitmap2=BitmapFactory.decodeStream(inputStream,null,options);
imageView.setImageBitmap(bitmap2);
SharedPreferences sharedPreferences=getSharedPreferences("info",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
//设置要存储的键值对
editor.putString("info",tempFullPath.getAbsolutePath());
//数据持久化
editor.commit();
}catch (Exception e){
}
}else {
Toast.makeText(TakePhotoActivity.this,"获取照片失败!",Toast.LENGTH_SHORT).show();
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_photo);
imageView=(ImageView)findViewById(R.id.imageViewPhoto);
textViewTitle = (TextView) findViewById(R.id.textViewTitle);
//得到保存的昵称
SharedPreferences sharedPreferences=getSharedPreferences("info",Context.MODE_PRIVATE);
String name=sharedPreferences.getString("name","none");
textViewTitle.setText(name);
//点击图片的事件
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// String action= MediaStore.ACTION_IMAGE_CAPTURE;
String action = "android.media.action.IMAGE_CAPTURE";
//创建文件目录
File dir=new File(Environment.getExternalStorageDirectory()+File.separator+"photo");
if (!dir.exists()){
dir.mkdir();
}
tempFullPath=new File(dir,System.currentTimeMillis()+".jpg");
Uri uri=Uri.fromFile(tempFullPath);
Intent intent = new Intent(action);
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
startActivityForResult(intent, 1);
}
});
//按钮的单击事件
Button button=(Button)findViewById(R.id.buttonChangePhoto);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//更换图片
/* String sdState=Environment.getExternalStorageState();
if (sdState.equals(Environment.MEDIA_MOUNTED)){
String path= Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator+"Nightscape.jpg";
Bitmap bitmap= BitmapFactory.decodeFile(path);
imageView.setImageBitmap(bitmap);
}*/
if (dialog==null){
AlertDialog.Builder builder=new AlertDialog.Builder(TakePhotoActivity.this);
//设置对话框参数
builder.setTitle("更换昵称");
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setNegativeButton("取消", null);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String newNameStr=editText.getText().toString();
textViewTitle.setText(newNameStr);
//通过SharedPreferences实现数据存储
SharedPreferences sharedPreferences=getSharedPreferences("info", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("name",newNameStr);
editor.commit();
}
});
//添加输入框
editText=new EditText(TakePhotoActivity.this);
builder.setView(editText);
//创建对话框
dialog=builder.create();
}
dialog.show();
}
});
//实现列表
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_take_photo, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
调用系统照相机,并解决内存溢出
最新推荐文章于 2018-10-26 15:43:04 发布