当前位置:  编程技术>移动开发
本页文章导读:
    ▪照相后裁剪        拍照后裁剪 private void doTakePhotoAction() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //Wysie_Soh: Create path for temp file mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory().........
    ▪ HOME键起动软件同点击图标进入软件区别        HOME键启动软件同点击图标进入软件区别 感谢nvstp分享按HOME键启动的软件的intent会带有*Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY*这个Flag 可以来区分软件是从History 列表启动,还是点击app icon启动 .........
    ▪ Cookies的封存       Cookies的保存 public class SerializedCookie implements Serializable { private static final long serialVersionUID = 5327445113190674523L; //arbitrary private String name; private String value; private String domain; public SerializedCookie(.........

[1]照相后裁剪
    来源: 互联网  发布时间: 2014-02-18
拍照后裁剪
private void doTakePhotoAction() {
    
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    //Wysie_Soh: Create path for temp file
    mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
                        "tmp_contact_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));

    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

    try {
        intent.putExtra("return-data", true);
        startActivityForResult(intent, PICK_FROM_CAMERA);
    } catch (ActivityNotFoundException e) {
        //Do nothing for now
    }
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_OK) {
        return;
    }

    switch (requestCode) {

    case CROP_FROM_CAMERA: {
        //Wysie_Soh: After a picture is taken, it will go to PICK_FROM_CAMERA, which will then come here
        //after the image is cropped.

        final Bundle extras = data.getExtras();

        if (extras != null) {
            Bitmap photo = extras.getParcelable("data");

            mPhoto = photo;
            mPhotoChanged = true;
            mPhotoImageView.setImageBitmap(photo);
            setPhotoPresent(true);
        }

        //Wysie_Soh: Delete the temporary file                        
        File f = new File(mImageCaptureUri.getPath());            
        if (f.exists()) {
            f.delete();
        }

        InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        mgr.showSoftInput(mPhotoImageView, InputMethodManager.SHOW_IMPLICIT);

        break;
    }

    case PICK_FROM_CAMERA: {
        //Wysie_Soh: After an image is taken and saved to the location of mImageCaptureUri, come here
        //and load the crop editor, with the necessary parameters (96x96, 1:1 ratio)

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

        intent.setData(mImageCaptureUri);
        intent.putExtra("outputX", 96);
        intent.putExtra("outputY", 96);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", true);            
        startActivityForResult(intent, CROP_FROM_CAMERA);

        break;

    }
    }
}

 具体可以参考

http://www.androidworks.com/crop_large_photos_with_android

Intent Details:

First, lets discuss this Intent, and its features.    After seeing some example code, I found that I could call it easily using an Intent like this:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType(“image/*”);
intent.putExtra(“crop”, “true”);

However, this is when I lacked additional documentation, what are the options, etc…  So, I have put together a table, and a demo program, in an attempt to demonstrated all available options from this control.  You may use my example code in your application, and expand upon it.  I’ll attach it to this post.

Exta Options Table for image/* crop:

 

SetExtra DataType Description crop String Signals the crop feature aspectX int Aspect Ratio aspectY int Aspect Ratio outputX int width of output created from this Intent outputY int width of output created from this Intent scale boolean should it scale return-data boolean Return the bitmap with Action=inline-data by using the data data Parcelable Bitmap to process, you may provide it a bitmap (not tested) circleCrop String if this string is not null, it will provide some circular cr MediaStore.EXTRA_OUTPUT ("output") URI Set this URi to a File:///, see example code

Now, the biggest confusion is going to be around this MediaStore.EXTRA_OUTPUT and return-data options.

You basically have 2 ways to get the bitmap back from this Intent, you can ask for it inline or provide a Uri that it can write to.

Option #1:  If you set return-data to "true", you will get back an Action = inline-data, and the bitmap returned will be inside the (Bitmap)extras.getParcelable("data").  Note:  If the image ends up being to large, you will get a nasty exception with this method, so you are restricted to keeping your outputX and outputY small.  In my code example attached, I did NOT use that method because of this reason.

Here is the code from CropImage.java (Android source code) where they write it inline:


// Return the cropped image directly or save it to the specified URI.
Bundle myExtras = getIntent().getExtras();
if (myExtras != null && (myExtras.getParcelable("data") != null
|| myExtras.getBoolean("return-data")))
{
Bundle extras = new Bundle();
extras.putParcelable("data", croppedImage);
setResult(RESULT_OK,(new Intent()).setAction("inline-data").putExtras(extras));
finish();

Option #2:  If you set return-data to "false", you will not receive a Bitmap back from the onActivityResult Intent in-line, instead you will need to set MediaStore.EXTRA_OUTPUT to a Uri (of File scheme only) where you want the Bitmap to be stored.  This has some restrictions, first you need to have a temp filesystem location in order to give the file scheme URI, not a huge problem (except on some devices that don't have sdcards).

Here is the code from CropImage.java (Android source code) where they write it to your Uri:


if (mSaveUri != null) {
OutputStream outputStream = null;
try {
outputStream = mContentResolver.openOutputStream(mSaveUri);
if (outputStream != null) {
croppedImage.compress(mOutputFormat, 75, outputStream);
}
} catch (IOException ex) {
// TODO: report error to caller
Log.e(TAG, "Cannot open file: " + mSaveUri, ex);
} finally {
Util.closeSilently(outputStream);
}
Bundle extras = new Bundle();
setResult(RESULT_OK, new Intent(mSaveUri.toString())
.putExtras(extras));
}
Example Code

I have attached some example code that should allow you to test various configurations.  Let me know if you find it useful (or not).D
Download here -> MediaStoreTest

From the code (the essential parts)


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
thiz = this;
setContentView(R.layout.main);
mBtn = (Button) findViewById(R.id.btnLaunch);
photo = (ImageView) findViewById(R.id.imgPhoto);

 

mBtn.setOnClickListener(new OnClickListener(){

public void onClick(View v) {
try {
// Launch picker to choose photo for selected contact
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", aspectX);
intent.putExtra("aspectY", aspectY);
intent.putExtra("outputX", outputX);
intent.putExtra("outputY", outputY);
intent.putExtra("scale", scale);
intent.putExtra("return-data", return_data);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection",!faceDetection); // lol, negative boolean noFaceDetection
if (circleCrop) {
intent.putExtra("circleCrop", true);
}

startActivityForResult(intent, PHOTO_PICKED);
} catch (ActivityNotFoundException e) {
Toast.makeText(thiz, R.string.photoPickerNotFoundText, Toast.LENGTH_LONG).show();
}
}
});

}

private Uri getTempUri() {
return Uri.fromFile(getTempFile());
}

private File getTempFile() {
if (isSDCARDMounted()) {

File f = new File(Environment.getExternalStorageDirectory(),TEMP_PHOTO_FILE);
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(thiz, R.string.fileIOIssue, Toast.LENGTH_LONG).show();
}
return f;
} else {
return null;
}
}

private boolean isSDCARDMounted(){
String status = Environment.getExternalStorageState();

if (status.equals(Environment.MEDIA_MOUNTED))
return true;
return false;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {
case PHOTO_PICKED:
if (resultCode == RESULT_OK) {
if (data == null) {
Log.w(TAG, "Null data, but RESULT_OK, from image picker!");
Toast t = Toast.makeText(this, R.string.no_photo_picked,
Toast.LENGTH_SHORT);
t.show();
return;
}

final Bundle extras = data.getExtras();
if (extras != null) {
File tempFile = getTempFile();
// new logic to get the photo from a URI
if (data.getAction() != null) {
processPhotoUpdate(tempFile);
}
}
}
break;
}
}


    
[2] HOME键起动软件同点击图标进入软件区别
    来源: 互联网  发布时间: 2014-02-18
HOME键启动软件同点击图标进入软件区别
感谢nvstp分享

按HOME键启动的软件的intent会带有*Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY*这个Flag
可以来区分软件是从History 列表启动,还是点击app icon启动

    
[3] Cookies的封存
    来源: 互联网  发布时间: 2014-02-18
Cookies的保存

public class SerializedCookie implements Serializable {

	private static final long serialVersionUID = 5327445113190674523L; //arbitrary

	private String name;
	private String value;
	private String domain;

	public SerializedCookie(Cookie cookie){
		this.name = cookie.getName();
		this.value = cookie.getValue();
		this.domain = cookie.getDomain();
	}

	public String getName(){
		return name;
	}

	public String getValue(){
		return value;
	}
	public String getDomain(){
		return domain;
	}
}

DefaultHttpClient client; //declared here, but get the client however you must. 

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		List<Cookie> cookies =client.getCookies();
		if (!cookies.isEmpty()){
			Cookie sessionInfo = cookies.get(0);
			outState.putSerializable("sessionInfo", new SerializedCookie(sessionInfo));
		}
DefaultHttpClient client; //declared here, but get the client however you must. 

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	if (client.getCookies().isEmpty()){
		if (savedInstanceState.containsKey("sessionInfo")){
			SerializedCookie cookie = (SerializedCookie) savedInstanceState.
				getSerializable("sessionInfo");

			BasicClientCookie newCookie = new BasicClientCookie(cookie.getName(),
					cookie.getValue());
					newCookie.setDomain(cookie.getDomain());

			client.addCookie(newCookie);
		} else {
                               //for whatever reason the session information couldn't be obtained,
                               //take action here
		}
	}


    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3