my code stock.com

Usando la cámara en android
(+)Sacar una foto (solo en landscape)
(+)Guardarla en la tarjeta de memoria
(+)Seleccionar una foto
(+)Mostrarla

Snippet options

Download: Download snippet as camera.java.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

package com.camera;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class CameraActivity extends Activity
{
    private static final int REQUEST_CAMERA = 1;
	private static final int REQUEST_SELECT_PHOTO = 0;
	private Button btn_camera;
	private Button btn_seleccionar;
	private Button btn_mostrar;	
	
	protected Uri imageUri;
	private String photoPath="";

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        btn_camera=(Button)findViewById(R.id.button1);
        btn_seleccionar=(Button)findViewById(R.id.button2);
        btn_mostrar=(Button)findViewById(R.id.button3);
        btn_camera.setOnClickListener(new OnClickListener() 
        {
			@Override
			public void onClick(View v) 
			{
				String state = Environment.getExternalStorageState();
	            if (Environment.MEDIA_MOUNTED.equals(state)) 
	            {					
	                long captureTime = System.currentTimeMillis();					
	                photoPath = Environment.getExternalStorageDirectory() + "/DCIM/Camera/Point" + captureTime + ".jpg";
	                try
	                {
	                    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
	                    File photo = new File(photoPath);
	                    intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photo));	                    
	                    startActivityForResult(Intent.createChooser(intent, "Capture Image"), REQUEST_CAMERA);
	                } 
	                catch (Exception e) 
	                {
	                    Toast.makeText(getApplicationContext(),       "Error",                    Toast.LENGTH_LONG).show();
	                    Log.e(e.getClass().getName(), e.getMessage(), e);
	                }
	            }
 
			}
		});
        btn_seleccionar.setOnClickListener(new OnClickListener() 
        {
			
			@Override
			public void onClick(View v) 
			{
				Intent ii = new Intent(Intent.ACTION_PICK) ;
        		ii.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        		MediaStore.Images.Media.CONTENT_TYPE) ;
        		startActivityForResult(ii,REQUEST_SELECT_PHOTO) ;	
			}
		});
        btn_mostrar.setOnClickListener(new OnClickListener() 
        {
			@Override
			public void onClick(View v) 
			{	

				if (photoPath!=null)
				{
					File imgFile = new  File(photoPath);				
					if(imgFile.exists());
					{
						Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
						ImageView myImage = (ImageView) findViewById(R.id.imageView1);						
						myImage.setImageBitmap(myBitmap);				
					}
				}
			}
        });
    }


		@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
		super.onActivityResult(requestCode, resultCode, data);
		
	    switch (requestCode)
	    {
		case REQUEST_CAMERA:		
								if (!photoPath.equals("")&&(photoPath!=null))
								{
									
									//oPunto.setPath(photoPath);
									//TODO mensaje de foto seleccionada
								}
								        		  
							
							break ;
		case REQUEST_SELECT_PHOTO:
			
							if( resultCode != 0 )
							{
							
								  Cursor c = managedQuery(data.getData(),null,null,null,null) ;
								  if( c.moveToFirst() )
								  {
							
									  photoPath = c.getString(1) ;
									
									  //oPunto.setPath(photoPath);
										//TODO mensaje de foto seleccionada
								  }
							 }
		default: break;
							 
		}
				 					
	}
		
	public static File convertImageUriToFile (Uri imageUri, Activity activity)  
	{
		Cursor cursor = null;
		try 
		{
		    String [] proj={MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION};
		    cursor = activity.managedQuery( imageUri,
		            proj, // Which columns to return
		            null,       // WHERE clause; which rows to return (all rows)
		            null,       // WHERE clause selection arguments (none)
		            null); // Order-by clause (ascending by name)
		    int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		    int orientation_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION);
		    if (cursor.moveToFirst()) 
		    {
		        String orientation =  cursor.getString(orientation_ColumnIndex);
		        Log.d("PingDroid", "OR "+orientation);
		        return new File(cursor.getString(file_ColumnIndex));
		    }
		    return null;
		} 
		catch (Exception ex)
		{			
			return null;
		}
		finally
		{
		    if (cursor != null) 
		    {
		        cursor.close();
		    }
		}
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) 
	{
      	outState.putString("foto", photoPath);
      	
	}

}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.