Archive for April, 2012

Capture The Image, Saved it in SDCard and Show it in to the Screen in Android

For saving the image in SDCard we have to first check that “is SDCard available or not?”

For checking the SDCard availability in android:

public static boolean isSdPresent()
{
      return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}

and The code for the image catpure and show is:

import java.io.ByteArrayOutputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.alishaik.capturedemo.common.FindSDCard;

public class StartUpActivity extends Activity
{
      private Button captureButton;
      private ImageView showImage;
      private Bitmap bitmap;
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);

         if(FindSDCard.isSdPresent())
         {
             showImage=(ImageView)findViewById(R.id.captured_img);

              captureButton=(Button)findViewById(R.id.button1);
              captureButton.setText("Open Camera");
              captureButton.setOnClickListener(new Button.OnClickListener()
              {
                  @Override
                  public void onClick(View v)
                  {
                      startCamera();
                  }
             });
         }
        else
        {
            Toast.makeText(this,"Insert SDCard", Toast.LENGTH_LONG).show();
        }
    }

    private void startCamera()
    {
       Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       intent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPath());
       startActivityForResult(intent, 1);
   }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode)
        {
           case -1:     Log.i("StartUpActivity", "Photo Captured");
                        bitmap=(Bitmap) data.getExtras().get("data");
                        MediaStore.Images.Media.insertImage(getContentResolver(),bitmap,null,null);
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                        showImage.setImageBitmap(bitmap);
                        break;
          default:      Log.i("StartUpActivity", "Cancelled Camera"); break;
      }
   }

    @Override
    public void onBackPressed()
    {
       super.onBackPressed();
    }

    public static boolean isSdPresent()
    {
        return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    }
}

and study this link:

http://developer.android.com/training/camera/photobasics.html#Taking Photos Simply

Get The Image From SDCard In Blackberry

For this we have to first check that “is SDCard available or not?”

For checking the SDCARD availability we have to use the following code:

Source code of SDCARD Checking: SDCardTest.java

import java.util.Enumeration;
import javax.microedition.io.file.FileSystemRegistry;
public class SDCardTest
{
 public static boolean isSDCardAvailable()
 {
 boolean sdCardPresent = false;
 String root = null;
 Enumeration e = FileSystemRegistry.listRoots();

 while (e.hasMoreElements())
 {
 root = (String)e.nextElement();
 if(root.equalsIgnoreCase("sdcard/"))
 {
 sdCardPresent = true;
 }
 }
 return sdCardPresent;
 }
}

and the code for Get the Image from SDCARD : LoadingScreen.java

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import net.rim.device.api.io.IOUtilities;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.picker.FilePicker;
import net.rim.device.api.ui.picker.FilePicker.Listener;

import com.hiddenbrains.image.common.SDCardTest;

public class LoadingScreen extends MainScreen implements FieldChangeListener
{
 private VerticalFieldManager ver;
 private ButtonField showImage;
 private BitmapField bitmapField;

 public LoadingScreen()
 {
        ver=new VerticalFieldManager(USE_ALL_WIDTH);
        showImage=new ButtonField("Show Image",Field.FIELD_HCENTER);
        showImage.setChangeListener(this);
        ver.add(showImage);

        bitmapField=new BitmapField(null,Field.FIELD_HCENTER);
        bitmapField.setPadding(10, 0, 10, 0);
       ver.add(bitmapField);

      add(ver);
 }
 public void fieldChanged(Field field, int context)
 {
 if(field==showImage)
 {
 selectImageFromSDCARD();
 }
 }

 private void selectImageFromSDCARD()
 {
 String PATH="";
 if(SDCardTest.isSDCardAvailable())
 PATH = System.getProperty("fileconn.dir.memorycard.photos");//The default stored Images Path;
 else
 PATH = System.getProperty("fileconn.dir.photos");//The default stored Images Path;

 FilePicker filePicker=FilePicker.getInstance();
 filePicker.setPath(PATH);
 filePicker.setListener(new Listener()
 {
 public void selectionDone(String url)
 {
 System.out.println("======================URL: "+url);
 try
 {
 FileConnection file = (FileConnection)Connector.open(url);
 if(file.exists())
 {
 InputStream inputStream = file.openInputStream();
 byte[] data=new byte[inputStream.available()];
 data=IOUtilities.streamToBytes(inputStream);
 Bitmap bitmap=Bitmap.createBitmapFromBytes(data, 0, data.length,1);//Here we get the Image;

 Bitmap scaleBitmap=new Bitmap(400, 300);//Now we are scaling that image;
 bitmap.scaleInto(scaleBitmap, Bitmap.FILTER_LANCZOS);
 bitmapField.setBitmap(scaleBitmap);
 }
 else
 {
 bitmapField.setBitmap(Bitmap.getBitmapResource("icon.png"));
 }
 }
 catch (IOException e)
 {
 bitmapField.setBitmap(Bitmap.getBitmapResource("icon.png"));
 }
 }
 });
 filePicker.show();
 }

 protected boolean onSavePrompt() //It doesn't show the "Save","Discard","Cancel" POPUP;
 {
 return true;
 }

 public boolean onMenu(int instance) //It doesn't show the Menu;
 {
 return true;
 }
}

Ali Shaik

Blackberry, IPhone and Android Application Developer;

BlogSpot: http://alishaik001.blogspot.in

Blackberry Dev Forum: http://supportforums.blackberry.com/t5/user/viewprofilepage/user-id/475839

StackOverFlow: http://stackoverflow.com/users/822327/alishaik786