首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在android手机中自动发送彩信或短信截图?2

如何在android手机中自动发送彩信或短信截图?2
EN

Stack Overflow用户
提问于 2014-07-13 03:20:51
回答 1查看 575关注 0票数 0

我已经创建了自己的相机应用程序。当我单击按钮时,它会拍摄图片并将其保存在图库中。我想要做的是拍照,然后自动发送一条彩信。现在我正在使用文件目录发送彩信,但它没有重放照片,只显示正常的文本。请帮我从这个代码发送照片。

代码语言:javascript
复制
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;
    }
EN

回答 1

Stack Overflow用户

发布于 2014-07-13 03:26:09

我使用jersey rest web服务实现了这一点。

这是我的Here服务:

代码语言:javascript
复制
@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中,你会这样做:

代码语言:javascript
复制
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();
        }
    }

如果图片很大,你不用担心,jersey会把图片分成块,然后把文件分成小块发送出去。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24716480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档