直接上例子吧,折腾了一上午,就整理出这点东西,这年头,知识挺贵的:)
package com.example.album; import java.io.ByteArrayOutputStream; import java.io.File; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class SysAlbumActivity extends Activity { public static final int NONE = 0; public static final int PHOTO_CAPTURE = 1; public static final int PHOTO_ALBUM = 2; public static final int PHOTO_CUT = 3; public static final String IMAGE_UNSPECIFIED = "image/*"; public ImageView imageView = null; public Button mAlbumBtn = null; public Button mCaptureBtn = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageView = (ImageView) findViewById(R.id.imageID); mAlbumBtn = (Button) findViewById(R.id.mAlbumBtn); mCaptureBtn = (Button) findViewById(R.id.mCaptureBtn); mAlbumBtn.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, IMAGE_UNSPECIFIED); startActivityForResult(intent, PHOTO_ALBUM); } }); mCaptureBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.jpg"))); startActivityForResult(intent, PHOTO_CAPTURE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == NONE) return; /** 拍照 */ if (requestCode == PHOTO_CAPTURE) { /** 设置文件保存路径这里放在跟目录下 */ File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg"); startPhotoZoom(Uri.fromFile(picture)); } if (data == null) return; /** 读取相册缩放图片 */ if (requestCode == PHOTO_ALBUM) { startPhotoZoom(data.getData()); } if (requestCode == PHOTO_CUT) { Bundle extras = data.getExtras(); if (extras != null) { Bitmap photo = extras.getParcelable("data"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 75, stream); imageView.setImageBitmap(photo); } } super.onActivityResult(requestCode, resultCode, data); } /** 进行图片的裁剪操作. */ public void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, IMAGE_UNSPECIFIED); intent.putExtra("crop", "true"); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY 是裁剪图片宽高 intent.putExtra("outputX", 64); intent.putExtra("outputY", 64); intent.putExtra("return-data", true); startActivityForResult(intent, PHOTO_CUT); } }
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//判断手机的语言,不能“==”!,要使用equals来判断
if( getLocaleLanguage().equals( "en-GB")|| getLocaleLanguage().equals( "en-US"))
{
// Log.d("123123", "en");
setContentView(R.layout.loading_en);
}
else
{
// Log.d("123123", "cn");
setContentView(R.layout.loading);
}
Handler hander=new Handler();
hander.postDelayed(new Runnable()
{
public void run()
{
finish();//这里直接关闭,不需要跳转,跳转的话会进入死循环
overridePendingTransition(R.anim.zoom_in,R.anim.zoom_out);
}
}
,3*1000);//设置3秒后结束
}
/**
* 屏蔽用户的返回键,即监听到返回键,但什么都不做
* */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK )
{
return false;//返回一个false,起到屏蔽的作用
}
return super.onKeyDown(keyCode, event);
}
/**
* getLocaleLanguage(),判断手机系统当前的语言
*
* 返回一個String
* */
public String getLocaleLanguage()
{
Locale l = Locale.getDefault();
return String.format("%s-%s", l.getLanguage(), l.getCountry());
}
String str = String.valueOf( totalBlocks * blockSize/1024/1024/1024+"GB");