使用xutils拍照从SD卡选图片上传

来源:互联网 发布:ipad保护套 知乎 编辑:程序博客网 时间:2024/05/17 23:24

xutils上传图片网上找了好多,demo比较少,而且都没有写一个清晰的流程,so。。自己写的

直接上代码;

public class MainActivity extends Activity {
//xutils的注解方法
@ViewInject(R.id.textView1) TextView textView1;
@ViewInject(R.id.button1) Button button1;
@ViewInject(R.id.button2) Button button2;
@ViewInject(R.id.button3) Button button3;
@ViewInject(R.id.button4) Button button4;
@ViewInject(R.id.imageView1) ImageView imageView1;
Bitmap bitmap;
private static String requestURL = "这里是你要上传数据的接口";
// 创建一个以当前系统时间为名称的文件,防止重复
private File file = new File(Environment.getExternalStorageDirectory(),
getPhotoFileName());


// 使用系统当前日期加以调整作为照片的名称
private String getPhotoFileName() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
return sdf.format(date) + ".png";
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//不多说。必须有
ViewUtils.inject(this);

}
@OnClick({R.id.button1,R.id.button2,R.id.button3,R.id.button4})
public void click(View v){
switch (v.getId()) {
case R.id.button1://拍照
pho();
break;
case R.id.button2://本地获取
local();
break;
case R.id.button3://显示信息
break;
case R.id.button4://上传图片
send();


break;
}
}
/**拍照*/
private void pho() {
// TODO Auto-generated method stub
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, 1);
}
/**从手机获取图片*/
private void local() {
// TODO Auto-generated method stub
Intent intent=new Intent(Intent.ACTION_PICK);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent, 2);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//resultCode结果返回码,如果返回RESULT_OK说明操作正常
if (resultCode!=RESULT_OK) {
return;
}
//判断是否有SD卡
if (Environment.getExternalStorageState().equals(Environment.MEDIA_UNKNOWN)) {
Toast.makeText(this, "SD卡不可用", 1000).show();
return;
}
//压缩图片的采样率,这里直接设定了,还有一种方法是根据屏幕计算
BitmapFactory.Options options=new Options();
options.inJustDecodeBounds=false;
options.inSampleSize=2;
switch (requestCode) {
case 1:
bitmap=BitmapFactory.decodeFile(file.getPath(),options);
break;
case 2:
if (data==null) {
return;
}
Uri uri=data.getData();
Cursor c=getContentResolver().query(uri, null, null, null, null);
if (c.moveToNext()) {
String path=c.getString(c.getColumnIndex(Images.Media.DATA));
Log.i("path", path);
file=new File(path);

bitmap=BitmapFactory.decodeFile(file.getPath(),options);
//这里曾经尝试过使用 oldfile.renameTo(new File(newpath))这种方法复制图片 ,有的时候可以运行,但是大多时候失败,网上查原因貌似是说和存储的形式有关,基本上就是看人品了。所以只能把图片另存处理了
saveMyBitmap(bitmap,file.getName());
}
break;
}
imageView1.setImageBitmap(null);//这个方法是网上看到的,说是可以在 重复操作的时候避免bitmap加载不上去
imageView1.setImageBitmap(bitmap);
textView1.setText("name="+file.getName()+"\n\n path="+file.getPath());
}
private void send() {
RequestParams params=new RequestParams();
//mime,我的理解是,图片是以流的形式上传到服务器的,而服务器还原图片时它的格式是什么?有的服务器自己会设定,有的就需要客户端上传一个要求,服务器根据这个要求还原图片的格式,而这个要求就是mime
String mime=MimeTypeMap.getSingleton().getMimeTypeFromExtension("png");
//upfile 根据接口的不同,这个值也不同,我的理解是;服务器规定,这个key对应的值,就是文件形式的
params.addBodyParameter("upfile", file,mime);

// params.addBodyParameter("key"," value");//如果上传文件的接口也支持上传其他的参数,就可以启用这个了
//请求方法是自己封装的,用的是post请求,参数是;接口、参数、回调方法
MyHttpUtils.parseShareJsonFromNet(requestURL, params, new JsonCallBack() {


@Override
public void callback(String jsonStr) {


}
});


}
/****  保存图片到SD卡***/
public void saveMyBitmap(Bitmap mBitmap,String bitName)  {
//定义另存文件的路径和名字、
       file= new File( Environment.getExternalStorageDirectory()+"/fei/",//后面加一个文件夹是为了避免把图片直接存在SD卡的大目录下,没来得及运行是否可以,貌似可以,不成的话就删了
    getPhotoFileName());
       FileOutputStream fOut = null;
       try {
               fOut = new FileOutputStream(file);
       } catch (FileNotFoundException e) {
               e.printStackTrace();
       }
       mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
       try {
               fOut.flush();
       } catch (IOException e) {
               e.printStackTrace();
       }
       try {
               fOut.close();
       } catch (IOException e) {
               e.printStackTrace();
       }
}
}




demo下载链接:http://download.csdn.net/detail/li419360214/9278903

0 0