package com.example.oldtab;

 import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import android.content.Intent;
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.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView; public class Group extends ActionBarActivity {
private static final int TAKE_PICTURE = 0x0; private static final String TAG_SEP = "----ninesofttestpostfile"; private ImageView imageview;
private Button button;
private Uri outputFileUri;
private Bitmap bitmap;
private File file; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group); imageview = (ImageView) findViewById(R.id.imageView1);
button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
File file = new File(Environment.getExternalStorageDirectory(),
"test.jpg");
outputFileUri = Uri.fromFile(file); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); startActivityForResult(intent, TAKE_PICTURE);
} }); } @Override
protected void onDestroy() {
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
}
super.onDestroy();
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE) {
if (data == null) {
if (bitmap != null) {
if (!bitmap.isRecycled()) {
bitmap.recycle();
}
}
int width = 1024;
int height = 768; file = new File(Environment.getExternalStorageDirectory(),
"test.jpg"); outputFileUri = Uri.fromFile(file); BitmapFactory.Options factoryoption = new BitmapFactory.Options();
factoryoption.inJustDecodeBounds = true; BitmapFactory
.decodeFile(outputFileUri.getPath(), factoryoption); int outwidth = factoryoption.outWidth;
int outheight = factoryoption.outHeight; int scale = Math.min(outwidth / width, outheight / height); factoryoption.inJustDecodeBounds = false;
factoryoption.inSampleSize = scale;
factoryoption.inPurgeable = true; bitmap = BitmapFactory.decodeFile(outputFileUri.getPath(),
factoryoption);
imageview.setImageBitmap(bitmap); saveToService();
}
}
super.onActivityResult(requestCode, resultCode, data);
} private void saveToService() {
new Thread(new Runnable() { @Override
public void run() {
try {
URL url = new URL("http://172.16.101.79/PostFile.ashx");
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + TAG_SEP);
conn.setDoOutput(true);
OutputStream out = conn.getOutputStream();
out.write(("--" + TAG_SEP + "\r\n").getBytes());
out.write(("Content-Disposition: form-data; name=\"postfile\"; filename=\"test.jpg\"\r\n")
.getBytes());
out.write(("Content-Type:image/jpeg\r\n\r\n").getBytes());
ByteArrayOutputStream fr = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fr);
out.write(fr.toByteArray());
out.write(("\r\n\r\n--" + TAG_SEP + "--\r\n").getBytes());
out.flush();
out.close();
conn.getResponseCode();
fr.close(); } catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} }).start(); }
}
 <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"
tools:context=".Group" > <ImageView
android:id="@+id/imageView1"
android:layout_width="500dp"
android:layout_height="300dp" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Button" /> </RelativeLayout>

Android开发之将拍摄的图片传至服务器的更多相关文章

  1. 图解android开发在界面上显示图片

    图解android开发在界面上显示图片<申明:转自百度> <原文章地址:http://jingyan.baidu.com/article/49711c6153a277fa441b7c ...

  2. Android开发常用开源框架:图片处理

    https://blog.csdn.net/SGQ_CSDN/article/details/79910709 Android开发常用开源框架:图片处理 框架名称 功能描述 Android Unive ...

  3. Android开发ImageView控件缩放图片

    首先还是最基础的ImageView控件如何显示图片: <ImageView                Android:id="@+id/imgView"          ...

  4. android开发里跳过的坑——图片文件上传失败

    使用的apache的httpclient的jar包,做的http图片上传,上传时,服务器总返文件格式不对.后来发现,是由于在创建FileBody时,使用了默认的ContentType引起的.所以服务器 ...

  5. 5.21学习总结——android开发实现用户头像的上传

    最近在做个人头像的上传,具体是能调用摄像头和从相册进行选择.本篇文章参考的我的同学的博客,大家有兴趣可以去原作者那里去看看: Hi(.・∀・)ノ (cnblogs.com) 1.使用glide进行图片 ...

  6. android 开发 获取各种intent (图片、apk文件、excel、pdf等文件)

    public static Intent openFile(String filePath){ File file = new File(filePath); if(!file.exists()) r ...

  7. Android开发之自定义圆角矩形图片ImageView的实现

    android中的ImageView只能显示矩形的图片,这样一来不能满足我们其他的需求,比如要显示圆角矩形的图片,这个时候,我们就需要自定义ImageView了,其原理就是首先获取到图片的Bitmap ...

  8. Android开发之自定义圆角矩形图片ImageView的实现 - Jamy Cai

    android中的ImageView只能显示矩形的图片,这样一来不能满足我们其他的需求,比如要显示圆角矩形的图片,这个时候,我们就需要自定义ImageView了,其原理就是首先获取到图片的Bitmap ...

  9. Android开发技巧——定制仿微信图片裁剪控件

    拍照--裁剪,或者是选择图片--裁剪,是我们设置头像或上传图片时经常需要的一组操作.上篇讲了Camera的使用,这篇讲一下我对图片裁剪的实现. 背景 下面的需求都来自产品. 裁剪图片要像微信那样,拖动 ...

随机推荐

  1. 关于mysql中information_schema.tables

    项目中出现这样一个SQL语句,现记录如下: @Select("select table_name tableName, engine, table_comment tableComment, ...

  2. .NET零基础入门10:打老鼠之数据存储

    一:数据库设计 到此为止,打老鼠游戏还不能保存每次游戏的成绩,我们今天完成的任务就是要存储成绩到SQLSERVER的数据库中. 在上节课中,我们已经知道了如何创建数据库,所有,先创建数据库" ...

  3. SIFT(Scale-invariant feature transform) & HOG(histogram of oriented gradients)

    SIFT :scale invariant feature transform HOG:histogram of oriented gradients 这两种方法都是基于图像中梯度的方向直方图的特征提 ...

  4. jpa命名规则 jpa使用sql语句 @Query

    关键字方法命名sql where字句 AndfindByNameAndPwdwhere name= ? and pwd =? OrfindByNameOrSexwhere name= ? or sex ...

  5. [leetcode]Add Binary @ Python

    原题地址:https://oj.leetcode.com/problems/add-binary/ 题意: Given two binary strings, return their sum (al ...

  6. 学习笔记:Maven的ArcheType的学习笔记

    摘要:     Archetype是什么?它由哪些文件组成?如何创建和安装自己的archtype,如何使用自己创建的archetype? 一.Archetype是什么     Archetype其实就 ...

  7. 自动化打包 Jenkins 持续集成 Git Gradle MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. (十一) 整合spring cloud云架构 - SSO单点登录之OAuth2.0登录流程(2)

    上一篇是站在巨人的肩膀上去研究OAuth2.0,也是为了快速帮助大家认识OAuth2.0,闲话少说,我根据框架中OAuth2.0的使用总结,画了一个简单的流程图(根据用户名+密码实现OAuth2.0的 ...

  9. CSS的50个代码片段

    1.css全局 html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a ...

  10. random_state 参数

    SVC(random_state=0)里有参数 random_state random_state 相当于随机数种子,下面会有代码来解释其作用.图中设置了 random.seed() 就相当于在 SV ...