如何在Android手机中自动发送mms或sms捕获照片? 2

时间:2014-07-12 19:20:51

标签: android

我创建了自己的相机应用程序。当我点击按钮时,它会拍摄照片并将其保存在图库中。我想要做的是拍照并自动发送一个mms。现在我使用文件目录发送mms但它没有重播照片只显示普通文本。请帮我发这个代码的照片。

private File getOutputMediaFile(int type) {
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.

        File mediaStorageDir = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                "MyCameraApp");

        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

        // Create the storage directory if it does not exist

        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                .format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_" + timeStamp + ".jpg");
        } else {
            return null;
        }

//      Intent sendIntent = new Intent(Intent.ACTION_SEND); 
//      sendIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
//      sendIntent.putExtra("sms_body", "5556"); 
//      sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(timeStamp));
//      sendIntent.setType("image/png");
//      startActivity(sendIntent); 

//        Intent smsIntent = new Intent(Intent.ACTION_VIEW);
//        smsIntent.setData(Uri.parse("smsto:"));
//        smsIntent.setType("vnd.android-dir/mms-sms");
//        smsIntent.putExtra("5556"  , "5556");
//        startActivity(smsIntent);
//        finish();
//      
        Intent sendIntent = new Intent(Intent.ACTION_SEND); 
        sendIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
        sendIntent.putExtra("sms_body", number); 
        sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mediaFile));
        sendIntent.setType("image/*");
        startActivity(sendIntent); 


        return mediaFile;
    }

1 个答案:

答案 0 :(得分:0)

我通过泽西休息网络服务来实现这一目标。

这是我的网络服务:

@POST
    @Path("/post/images")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response imageUpload(@FormDataParam("image") InputStream hereIsImage, @FormDataParam("image") FormDataContentDisposition hereIsName) {
        String path = System.getenv("HOME")+"/tmp/";
        if(hereIsName.getSize()==0) {
            return Response.status(500).entity("image parameter is missing").build();
        }
        String name = hereIsName.getFileName();
        path += name;

        try {
            OutputStream out = new FileOutputStream(new File(name));
            int read;

            byte[] bytes = new byte[1024];
            while ((read = hereIsImage.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            out.flush();
            out.close();
        } catch (IOException e) {
            return Response.status(500).entity(name + " was not uploaded\n"+e.getMessage()).build();
        }
        return Response.status(200).entity(name + " was uploaded").build();
    }

在Android中你会做这样的事情:

public void makeImagePost(byte[] image, String resource, String url) {
        try {
            httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost(resource);
            ByteArrayBody bab = new ByteArrayBody(image, url+".jpg");
            MultipartEntity reqEntity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("image", bab);
            postRequest.setEntity(reqEntity);
            HttpResponse response = httpClient.execute(postRequest);
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent(), "UTF-8"));
            String sResponse;
            StringBuilder s = new StringBuilder();
            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }
            System.out.println("Response: " + s);
        } catch (Exception e) {
            // handle exception here
            e.printStackTrace();
        }
    }

你不必担心,如果图像是非常大的球衣将分开块并将文件分片发送。