求解答,安卓开发从app中拍照传至服务器失败

提问,新人安卓程序有两个问题:
1、安卓前端点击拍照触发camera()方法执行拍照,但是获得到的照片非常模糊
1、拍的到照片上传至服务器,但是上传失败

//执行拍照触发事件

private static final int REQUEST_CODE_TAKE_PICTURE = 1000;
    public static final String TAG = "MainActivity";
    public File file_picture = null;

    public void camera(View v) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
        }

//拍照完成后执行结果

 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == RESULT_OK) {
	           super.onActivityResult(requestCode, resultCode, intent);
	           if (resultCode == Activity.RESULT_OK) { //RESULT_OK==1
	               String sdStatus = Environment.getExternalStorageState();
	               if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
	                   Log.i("TestFile",
	                           "SD card is not avaiable/writeable right now.");
	                   return;
	               }
	               new DateFormat();
	               String name = DateFormat.format("_yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";
	               String imageName = name;

	               bundle = intent.getExtras();        // 获得页面传递过来的参数数组
	               Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
	               if (!bitmap.equals(null)) {
	                   System.out.println("获取相机数据成功");
	               } else {
	                   System.out.println("获取相机数据失败");
	               }
	               FileOutputStream b = null;
	               file_picture = new File("/sdcard/Image/");

	               if (!file_picture.exists()) {
	                   file_picture.mkdirs();// 创建文件夹
	               }
	
	               String fileName = "/sdcard/Image/" + imageName;
	               try {
	                   b = new FileOutputStream(fileName);
	                   bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
	               } catch (FileNotFoundException e) {
	                   e.printStackTrace();
	               } finally {
	                   try {
	                       b.flush();
	                       b.close();
	                   } catch (IOException e) {
	                       e.printStackTrace();
	                   }
	                   if (!fileName.equals(null)) {
	                       System.out.println("图片插入成功");
	                   } else {
	                       System.out.println("图片插入失败");
	                   }
	                   sendImage(fileName);//发送图片
	               }
	           }

            }
        }
    }

//执行发送图片

    private void sendImage(String fileName) {
        Bitmap bm = BitmapFactory.decodeFile(fileName);//打开路径下的图片
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] bytes = stream.toByteArray();

        //将图片的字节流数据加密成base64字符输出
        String img = new String(Base64.encodeToString(bytes, Base64.DEFAULT));
        RequestParams params = new RequestParams();
        
        String nameActual = fileName.substring(fileName.lastIndexOf("/") + 1);
        params.add("Pic_1", img);
        params.add("name", nameActual);

        String URL = FirePlugApp.POSTPICTURE_SERVER_URL + "/UploadPic";

        com.loopj.android.http.AsyncHttpClient client = new com.loopj.android.http.AsyncHttpClient();


        client.post(URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Toast.makeText(ReportData.this, "图片上传成功!", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Toast.makeText(ReportData.this, "图片上传失败!", Toast.LENGTH_LONG).show();
            }
        });
    }

//服务器端的接口

package com.starwsn.kiteway;

import sun.misc.BASE64Decoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class UploadPic extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");

        String photo=req.getParameter("Pic_1");
        if (!photo.equals(null)){
            System.out.println("Pic_1数据接收成功");
        }else{
            System.out.println("Pic_1数据接收失败");
        }

        String photoName=req.getParameter("name");
        if (!photoName.equals(null)){
        System.out.println("name数据接收成功");
        }else {
            System.out.println("name数据接收失败");
        }

        // 对base64数据进行解码 生成 字节数组,不能直接用Base64.decode();进行解密
        byte[] photoimg = new BASE64Decoder().decodeBuffer(photo);
        for (int i = 0; i < photoimg.length; ++i) {
            if (photoimg[i] < 0) {
                // 调整异常数据
                photoimg[i] += 256;
            }
        }

        System.out.println("图片的大小:" + photoimg.length);
        File file = new File("e:", "decode.png");
        File filename = new File("e:\\name.txt");
        if (!filename.exists()) {
            file.createNewFile();
        }
        if (!file.exists()) {
            file.createNewFile();
        }

        FileOutputStream out = new FileOutputStream(file);
        FileOutputStream out1 = new FileOutputStream(filename);
        out1.write(photoName.getBytes());
        out.write(photoimg);
        out.flush();
        out.close();
        out1.flush();
        out1.close();

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值