Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build a photo upload application in android [duplicate]

Tags:

android

Possible Duplicate:
Calling camera from an activity, capturing an image and uploading to a server

I need to build an application that will start the camera, take a photo, save that phto to the sdcard, and then upload this photo to a .net server without altering it's quality, any one got an idea?

like image 982
Hassan Mokdad Avatar asked Dec 10 '22 08:12

Hassan Mokdad


2 Answers

you already wrote the solution ^^ To start the camera app use:

    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); 

Then you need a 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;

    }   
}

After storing the Picture you have to perform a Post Request to load the picture in the web, you need script wich is copying the file to the server, maybe asp.net and than you only have to perform the Request. I only have a code for https Requests with credentials, using a External Libary from appache, this might be a little bit too complicated, but I'm sure you will finde a code here, otherwise my solution looks like:

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;
        } 

} 
like image 92
2red13 Avatar answered Dec 11 '22 22:12

2red13


for taking photo use this code

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);

for save ur photo

@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);

            }

        }

finally upload photo to server try using ksoap webservices

like image 38
Kakey Avatar answered Dec 11 '22 22:12

Kakey