【问题标题】:Build a photo upload application in android [duplicate]在android中构建照片上传应用程序[重复]
【发布时间】:2011-05-18 09:19:45
【问题描述】:

可能重复:
Calling camera from an activity, capturing an image and uploading to a server

我需要构建一个应用程序来启动相机、拍照、将该照片保存到 SD 卡中,然后将该照片上传到 .net 服务器而不改变其质量,有人知道吗?

【问题讨论】:

  • 你是什么意思:“有人有想法吗?”。我们不会为您做,如果您有具体问题,我们可以为您提供帮助,仅此而已。

标签: android


【解决方案1】:

您已经编写了解决方案 ^^ 要启动相机应用,请使用:

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    captured_image = System.currentTimeMillis() + ".jpg";
    File file = new File(Environment.getExternalStorageDirectory(), captured_image); 
    captured_image = file.getAbsolutePath();
    Uri outputFileUri = Uri.fromFile(file); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
    intent.putExtra("return-data", true);
    ((Activity) GlobalVars.main_ctx).startActivityForResult(intent, RES_IMAGE_CAPTURE); 

那么你需要一个ActivityResulListener Like:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    switch (requestCode) { 
        case RES_IMAGE_CAPTURE: 

            Log.i( "MakeMachine", "resultCode: " + resultCode );
            switch( resultCode )
            {
                case 0:
                    Log.i( "MakeMachine", "User cancelled" );
                    break;
                case -1:
                    //image storead, now load it in the web
                    break;
                }
            break;

    }   
}

存储图片后,您必须执行发布请求以将图片加载到网络中,您需要将文件复制到服务器(可能是 asp.net)的脚本,而您只需执行请求即可。我只有一个带有凭据的 https 请求代码,使用来自 appache 的外部库,这可能有点太复杂了,但我相信你会在这里找到代码,否则我的解决方案如下所示:

public static boolean upload_image(String url, List<NameValuePair> nameValuePairs,String encoding) {

    DefaultHttpClient http = new DefaultHttpClient();
        SSLSocketFactory ssl =  (SSLSocketFactory)http.getConnectionManager().getSchemeRegistry().getScheme( "https" ).getSocketFactory(); 
        ssl.setHostnameVerifier( SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER );
        final String username = "username";
        final String password = "password";
        UsernamePasswordCredentials c = new UsernamePasswordCredentials(username,password);
        BasicCredentialsProvider cP = new BasicCredentialsProvider(); 
        cP.setCredentials(AuthScope.ANY, c); 
        http.setCredentialsProvider(cP);
        HttpResponse res;
        try {
            HttpPost httpost = new HttpPost(url);
            MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT); 

            for(int index=0; index < nameValuePairs.size(); index++) { 
                ContentBody cb;
                if(nameValuePairs.get(index).getName().equalsIgnoreCase("File")) { 
                    File file = new File(nameValuePairs.get(index).getValue());
                    FileBody isb = new FileBody(file,"application/*");
                    entity.addPart(nameValuePairs.get(index).getName(), isb);
                } else { 
                    // Normal string data 
                    cb =  new StringBody(nameValuePairs.get(index).getValue(),"", null);
                    entity.addPart(nameValuePairs.get(index).getName(),cb); 
                } 
            } 


            httpost.setEntity(entity);
            res = http.execute(httpost);

            InputStream is = res.getEntity().getContent();
            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;
            while((current = bis.read()) != -1){
                  baf.append((byte)current);
             }
            res = null;
            httpost = null;
            String ret = new String(baf.toByteArray(),encoding);
            GlobalVars.LastError = ret;
            return  true;
           } 
        catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            return true;
        } 
        catch (IOException e) {
            // TODO Auto-generated catch block
            return true;
        } 

} 

【讨论】:

    【解决方案2】:

    使用此代码拍照

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File file = new File(Environment.getExternalStorageDirectory(), "test.jpg");
            outputFileUri = Uri.fromFile(file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(intent, TAKE_PICTURE);
    

    保存你的照片

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data){
    
            if (requestCode == TAKE_PICTURE)
            {
                //Uri contentURI = Uri.parse(data.getDataString()); 
    
                ContentResolver cr = getContentResolver();
                InputStream in = null;
                try 
                {
                    in = cr.openInputStream(outputFileUri); 
                    Log.i("URI ===> ", outputFileUri.getPath());
                } 
                catch (FileNotFoundException e) 
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if(in!=null)
                {
    
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize=8;
                    bit = BitmapFactory.decodeStream(in,null,options);
    
                }
    
            }
    

    最后上传照片到服务器尝试使用 ksoap webservices

    【讨论】:

    • 谢谢,onActivityResult 部分帮助了我。
    【解决方案3】:

    您可以使用拍照意图拍照

    startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), TAKE_PICTURE);
    

    也可以参考http://developer.android.com/reference/android/provider/MediaStore.html

    【讨论】:

      【解决方案4】:

      参考 Android 示例,有很多相机使用示例和 http 示例。

      http://developer.android.com/reference/android/hardware/Camera.html http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html

      试试谷歌,有很多例子。如果您遇到特定问题,请询问。

      祝你好运。

      【讨论】:

        猜你喜欢
        • 2011-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-29
        • 2022-11-18
        • 1970-01-01
        • 2012-06-19
        • 1970-01-01
        相关资源
        最近更新 更多