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. Java NIO 的前生今世 之四 NIO Selector 详解

    Selector Selector 允许一个单一的线程来操作多个 Channel. 如果我们的应用程序中使用了多个 Channel, 那么使用 Selector 很方便的实现这样的目的, 但是因为在一 ...

  2. Java NIO: Non-blocking Server

    Even if you understand how the Java NIO non-blocking features work (Selector, Channel, Buffer etc.), ...

  3. git: error while loading shared libraries: libiconv.so.2

    git安装之后出现:git: error while loading shared libraries: libiconv.so.2: cannot open shared object file: ...

  4. [leetcode]Best Time to Buy and Sell Stock @ Python

    原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/ 题意: Say you have an array for ...

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

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

  6. SQL SERVER CXPACKET-Parallelism Wait Type 的惯用解决方案

    最近我的两个库出现,出现较多的CXPACKET等待,在网上找了一下资料.其中有篇一个SQL Server专栏作家的文章不错,也解决了我的一些疑问,就翻译在这里. 翻译整理仅用于传播资讯之目的. 原文出 ...

  7. seleium 鼠标悬停事件

    seleium 教程:https://www.yiibai.com/selenium seleium官网:https://www.seleniumhq.org/docs/ 1.鼠标悬停 例如,下图 鼠 ...

  8. javascript学习笔记——怎样改动&lt;a href=&quot;#&quot;&gt;url name&lt;/a&gt;

    0.前言     使用了一段时间javascript,再花了点时间学习了jquery.可是总是感觉自己非常"迷糊",比如<a href="#">ur ...

  9. laravel 开启sql调试

    打开app\Providers\AppServiceProvider.PHP,在boot方法中添加如下内容 public function boot() { //sql调试 $sql_debug = ...

  10. Ubuntu SVN安装&使用&命令

    SVN 安装 apt-get install subversion checkout svn checkout svn://192.168.1.110/app 按提示输入相应的用户名和密码. 往版本库 ...