2

I want to develop app that take a picture and then store it on a ImageView like a preview image. and after i want to save the uri path to a dataBase . i know how to take a picture and how to save it on my sd Card but how can i get the specific image to save it on my database table or to create a preview on imageView real time ? here my code:

public class Add_item extends Activity 
                      implements SurfaceHolder.Callback , OnClickListener {

    EditText d_edName,d_edPrice,d_edInfo;
    ImageView d_image_pre1,d_image_pre2,d_image_pre3;
    Button d_BTakePicture, d_bSave;

    Camera camera;
    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;

    private PictureCallback rawCallback;
    private ShutterCallback shutterCallback;
    private PictureCallback jpegCallback;
    private String tag;

    private File myGeneralFolder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_additem);

        d_edName = (EditText) findViewById(R.id.d_name);

        d_edPrice = (EditText) findViewById(R.id.d_price);
        d_edInfo = (EditText)findViewById(R.id.d_info);
        d_image_pre1 = (ImageView) findViewById(R.id.d_image1);
        d_image_pre2 = (ImageView) findViewById(R.id.d_image2);
        d_image_pre3 = (ImageView) findViewById(R.id.d_image3);

        d_BTakePicture = (Button) findViewById(R.id.d_bTakePicture);
        d_BTakePicture.setOnClickListener(this);
        d_bSave = (Button) findViewById(R.id.d_bSave);
        d_bSave.setOnClickListener(this);

        surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
        surfaceHolder = surfaceView.getHolder();

        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        rawCallback = new PictureCallback() {
            public void onPictureTaken(byte[] data, Camera camera) {
                Log.d("Log", "onPictureTaken - raw");
            }
        };

        shutterCallback = new ShutterCallback() {
            public void onShutter() {
                Log.i("Log", "onShutter'd");
            }
        };

        jpegCallback = new PictureCallback() {
            public void onPictureTaken(byte[] data, Camera camera) {
                FileOutputStream outStream = null;

                try {
                    outStream = new FileOutputStream(String.format(myGeneralFolder+"/%d.jpg",(System.currentTimeMillis())));
                    outStream.write(data); 
                    outStream.close();

                    Log.d("Log", "onPictureTaken - wrote bytes: " + data.length);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                }
                Log.d("Log", "onPictureTaken - jpeg");
            }
        };    
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.d_bSave:
            String name = d_edName.getText().toString();
            String price = d_edPrice.getText().toString();
            String info = d_edInfo.getText().toString();

            Handler hand = new Handler(Add_item.this);
            hand.open();
            hand.addItem(new Item(0, name, info, "", price));
            hand.close();
            start_camera();
            break;

        case R.id.d_bTakePicture:
            if(d_edName.getText().toString().equals("")){
                Toast.makeText(Add_item.this, "No item name, folder cannot created!", 2000).show();
            }else{
            myGeneralFolder = new  File(Environment.getExternalStorageDirectory()+File.separator+"mySalesImages"+File.separator+d_edName.getText().toString());
            myGeneralFolder.mkdirs();
            captureImage(); 
            }
            break;

        default:
            break;
        }       
    }

    private void start_camera()
    {
            try{
                camera = Camera.open();
            }catch(RuntimeException e){
                Log.e(tag, "init_camera: " + e);
                return;
            }
            Camera.Parameters param;
            param = camera.getParameters();
            //modify parameter
            param.setPreviewFrameRate(20);
            param.setPreviewSize(176, 144);
            camera.setParameters(param);
            try {
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
                //camera.takePicture(shutter, raw, jpeg)
            } catch (Exception e) {
                Log.e(tag, "init_camera: " + e);
                return;
            }
     }

    private void stop_camera()
    {
          camera.stopPreview();
          camera.release();
    }

    private void captureImage() {
          // TODO Auto-generated method stub
          camera.takePicture(shutterCallback, rawCallback, jpegCallback);       
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
}

My error:

06-17 19:12:32.416: W/System.err(23714): java.net.MalformedURLException: Protocol not found: /storage/emulated/0/mySalesImages/hggg/1403021552392.jpg
06-17 19:12:32.426: W/System.err(23714):    at java.net.URL.<init>(URL.java:178)
06-17 19:12:32.426: W/System.err(23714):    at java.net.URL.<init>(URL.java:127)
06-17 19:12:32.426: W/System.err(23714):    at com.example.ordersapp.Add_item$3.onPictureTaken(Add_item.java:115)
06-17 19:12:32.426: W/System.err(23714):    at android.hardware.Camera$EventHandler.handleMessage(Camera.java:851)
06-17 19:12:32.426: W/System.err(23714):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 19:12:32.426: W/System.err(23714):    at android.os.Looper.loop(Looper.java:137)
06-17 19:12:32.426: W/System.err(23714):    at android.app.ActivityThread.main(ActivityThread.java:5419)
06-17 19:12:32.426: W/System.err(23714):    at java.lang.reflect.Method.invokeNative(Native Method)
06-17 19:12:32.426: W/System.err(23714):    at java.lang.reflect.Method.invoke(Method.java:525)
06-17 19:12:32.426: W/System.err(23714):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
06-17 19:12:32.426: W/System.err(23714):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
06-17 19:12:32.426: W/System.err(23714):    at dalvik.system.NativeStart.main(Native Method)
Matan
  • 296
  • 5
  • 24

2 Answers2

0

Not sure what exactly you mean but thats how you set the file path :

FileOutputStream outStream = new FileOutputStream(path + filename,true);

so in your example :

outStream = new FileOutputStream(String.format(myGeneralFolder+"/%d.jpg",(System.currentTimeMillis())));

this String.format(myGeneralFolder+"/%d.jpg" will the path I guess. so assing it to a viariable and use the variable.

go ahead and do this :

    try{    
    String currentPicPath = String.format(myGeneralFolder+"/%d.jpg",(System.currentTimeMillis()));
        ImageView i = (ImageView)findViewById(R.id.image);
        Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(currentPicPath ).getContent());
        i.setImageBitmap(bitmap);
 } catch (MalformedURLException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}

good luck

Your question is also answered here : Android, Make an image at a URL equal to ImageView's image

Community
  • 1
  • 1
Zardaloop
  • 1,594
  • 5
  • 22
  • 43
  • Yes this is the path . but i cant set this path on imageView. Example **String currentPicPath = String.format(myGeneralFolder+"/%d.jpg",(System.currentTimeMillis())); outStream = new FileOutputStream(currentPicPath); outStream.write(data); outStream.close(); uriImage = Uri.parse(currentPicPath); d_image_pre1.setImageURI(uriImage);** – Matan Jun 17 '14 at 14:50
  • you should go through debugger and step over the currentpucPath and see what is the path. what is the path when you debug it ? – Zardaloop Jun 17 '14 at 15:00
  • /storage/emulated/0/mySalesImages/bikes/1403019198320.jpg – Matan Jun 17 '14 at 15:33
  • ok matt I have edited my answer. get the imageviewer by ID, then turn the URL into bitmap them assign the image view with image bitmap. This should work. – Zardaloop Jun 17 '14 at 15:38
  • Thank your answer ,but it still dont working. :( it is not set the imageView – Matan Jun 17 '14 at 15:59
  • 06-17 19:48:34.501: W/OpenGLRenderer(31526): Bitmap too large to be uploaded into a texture (4128x2322, max=4096x4096) – Matan Jun 17 '14 at 16:49
  • in your code you above you are not applying what i have suggested you. Also have a look at this : http://stackoverflow.com/questions/22477508/openglrenderer-bitmap-too-large-to-be-uploaded-into-a-texture-in-android-titani – Zardaloop Jun 18 '14 at 08:48
  • thank you all but i fix it with that solution : " BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; Bitmap bm = BitmapFactory.decodeFile(pathHere, options); image.setImageBitmap(bm);" – Matan Jun 18 '14 at 11:03
0

You can find example with full explanation in Android documentation here http://developer.android.com/guide/topics/media/camera.html how to start camera with intent, how to listen for result and what sort of data to expect in result

peter_budo
  • 1,748
  • 4
  • 26
  • 48
  • Thank you but i use custom camera with surfaceView , and i want couple of pictures in the same time. – Matan Jun 17 '14 at 16:05