码迷,mamicode.com
首页 > Web开发 > 详细

webview压缩图片

时间:2016-04-17 20:41:44      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

anim

popupwindow_anim

<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
android:duration="500"
android:fromYDelta="100%p"
android:toYDelta="0" />

<alpha
android:duration="500"
android:fromAlpha="0.0"
android:toAlpha="1.0" />

</set>

 
   

popupwindow_anim1

<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromYDelta="0"
android:toYDelta="50%p" />

<alpha
android:duration="500"
android:fromAlpha="1.0"
android:toAlpha="0.0" />

</set>

 
   

values里面的styles

<!-- 这个是加入的代码 -->
<style name="mypopwindow_anim_style">
<item name="android:windowEnterAnimation">@anim/popupwindow_anim</item>

<item name="android:windowExitAnimation">@anim/popupwindow_anim1</item>

</style>

 
   

activity.main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main"
tools:context=".MainActivity" >

<WebView
android:id="@+id/wv"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>

 
   

poput

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/pop_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:orientation="vertical" >

<Button
android:id="@+id/btn_take_photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip"
android:text="拍照"
android:textStyle="bold" />

<Button
android:id="@+id/btn_pick_photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="5dip"
android:text="从相册选择"
android:textStyle="bold" />

<Button
android:id="@+id/btn_cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="15dip"
android:text="取消"
android:textColor="#ffffff"
android:textStyle="bold" />
</LinearLayout>

</RelativeLayout>

 
   

mainactivity.java

package com.bwie.webviewupload;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
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.util.Base64;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

public class MainActivity extends Activity {

private ValueCallback<Uri> mUploadMessage;
public static final int NONE = 0;
public static final int PHOTO_CAMERA = 1;// 相机拍照
public static final int PHOTO_COMPILE = 2; // 编辑图片
public static final int PHOTO_RESOULT = 3;// 结果
private String ImageName;
Bitmap myBitmap;
private byte[] mContent;

public static String getStringToday() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
String dateString = formatter.format(currentTime);
return dateString;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// 找控件
final WebView wv = (WebView) findViewById(R.id.wv);

// 设置js有效
wv.getSettings().setJavaScriptEnabled(true);

// 为wv加载网络页面
wv.loadUrl("http://101.200.142.201/Login/register.html");

// 为wv设置js交互
wv.setWebChromeClient(new WebChromeClient() {

// 进行上传
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType, String capture) {

mUploadMessage = uploadMsg;

// 在下方弹出poppupWindow
MyPopupWindow pop = new MyPopupWindow(MainActivity.this,
new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 判断
switch (v.getId()) {
case R.id.btn_pick_photo:

// 设置调用系统相册的意图(隐式意图)
Intent intent = new Intent();

// 设置值活动//android.intent.action.PICK
intent.setAction(Intent.ACTION_PICK);

// 设置类型和数据
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");

// 开启系统的相册
startActivityForResult(intent,
PHOTO_COMPILE);

break;

case R.id.btn_take_photo:

// 设置图片的名称
ImageName = "/" + getStringToday() + ".jpg";

// 设置调用系统摄像头的意图(隐式意图)
Intent intent1 = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);

// 设置照片的输出路径和文件名

File file = new File(Environment
.getExternalStorageDirectory(),
ImageName);

intent1.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(file));
// 开启摄像头
startActivityForResult(intent1,
PHOTO_CAMERA);

break;
case R.id.btn_cancel:

break;

}

}
});

// 显示窗口 main 为mainactivity的主控件
pop.showAtLocation(MainActivity.this.findViewById(R.id.main),
Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 设置layout在PopupWindow中显示的位置

}

});

}

// 调用startActivityResult,返回之后的回调函数
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

ContentResolver resolver = getContentResolver();
// 裁剪照片的处理结果
if (requestCode == PHOTO_COMPILE) {

String path = getAbsoluteImagePath(data.getData());
Bitmap bitmap = getimage(path);
Uri imageUri = Uri.parse(MediaStore.Images.Media.insertImage(
getContentResolver(), bitmap, null, null));

Uri result = data == null || resultCode != RESULT_OK ? null
: data.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;

}

if (requestCode == PHOTO_CAMERA)// 拍照
{
// 设置文件保存路径这里放在跟目录下
File picture = new File(Environment.getExternalStorageDirectory()
+ ImageName);

Bitmap bitmap1 = getimage(picture.getPath());

Uri imageUri = Uri.parse(MediaStore.Images.Media.insertImage(
getContentResolver(), bitmap1, null, null));
mUploadMessage.onReceiveValue(imageUri);
mUploadMessage = null;

}

super.onActivityResult(requestCode, resultCode, data);
}

// http://blog.csdn.net/cherry609195946/article/details/9264409
// android图片压缩总结
// // 图片按比例大小压缩方法(根据Bitmap图片压缩)
// 压缩图片url
private Bitmap getimage(String srcPath) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
// 开始读入图片,此时把options.inJustDecodeBounds 设回true了
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此时返回bm为空

newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
// 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
float hh = 800f;// 这里设置高度为800f
float ww = 480f;// 这里设置宽度为480f
// 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;// be=1表示不缩放
if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;// 设置缩放比例
// 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
return compressImage(bitmap);// 压缩好比例大小后再进行质量压缩
}

private Bitmap compressImage(Bitmap image) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 100;
while (baos.toByteArray().length / 1024 > 100) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
baos.reset();// 重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中
options -= 10;// 每次都减少10
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
return bitmap;
}

protected String getAbsoluteImagePath(Uri uri) {
// can post image
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)

int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();

return cursor.getString(column_index);
}

}

 
   

 

MyPopupWindow

package com.bwie.webviewupload;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

public class MyPopupWindow extends PopupWindow {

private Button btn_take_photo, btn_pick_photo, btn_cancel;
private View mMenuView;

public MyPopupWindow(Activity context, OnClickListener itemsOnClick) {
super();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView = inflater.inflate(R.layout.popup, null);
btn_take_photo = (Button) mMenuView.findViewById(R.id.btn_take_photo);
btn_pick_photo = (Button) mMenuView.findViewById(R.id.btn_pick_photo);
btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
// 取消按钮
btn_cancel.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// 销毁弹出框
dismiss();
}
});

// 设置按钮监听
btn_pick_photo.setOnClickListener(itemsOnClick);
btn_take_photo.setOnClickListener(itemsOnClick);
// 设置SelectPicPopupWindow的View
this.setContentView(mMenuView);
// 设置SelectPicPopupWindow弹出窗体的宽
this.setWidth(LayoutParams.FILL_PARENT);
// 设置SelectPicPopupWindow弹出窗体的高
this.setHeight(LayoutParams.WRAP_CONTENT);
// 设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
// 设置SelectPicPopupWindow弹出窗体动画效果
this.setAnimationStyle(R.style.mypopwindow_anim_style);
// 实例化一个ColorDrawable颜色为半透明
// ColorDrawable dw = new ColorDrawable(0xb0000000);
// 设置SelectPicPopupWindow弹出窗体的背景
// this.setBackgroundDrawable(dw);
// mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
mMenuView.setOnTouchListener(new OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {

int height = mMenuView.findViewById(R.id.pop_layout).getTop();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP) {
if (y < height) {
dismiss();
}
}
return true;
}
});
}

}

 
   

加权限

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 
   

webview压缩图片

标签:

原文地址:http://www.cnblogs.com/nanze/p/5401860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!