0

I am new to android i want to make an application of list of image in which i want save image from camera to data base and retrieve from database.I want to do this by saving uri of database .I did it in following way 1. I am saving image in file and set it in image view further 2. I am going to save images uri in database. but i am stuck in first step it self .please help me.

package com.example.camerademo;

import java.io.File;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;

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

public class MainActivity extends Activity {
Button click;
ImageView image;
Intent intent;
final static int requestCode=100;
private static final int MEDIA_TYPE_IMAGE = 1;
private static final int MEDIA_TYPE_VIDEO = 2;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        click= (Button) findViewById(R.id.clickbutton);
        image= (ImageView) findViewById(R.id.imageholder);
        intent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);



        Uri mediauri= getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mediauri);
        click.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                startActivityForResult(intent, requestCode);

            }
        });

    }
    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);




            if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
                if (resultCode == RESULT_OK) {
                    // Image captured and saved to fileUri specified in the Intent
                    Toast.makeText(this, "Image saved to:\n" +
                             data.getData(), Toast.LENGTH_LONG).show();
                    image.setImageURI(Uri.parse(data.getStringExtra(MediaStore.EXTRA_OUTPUT)));
                } else if (resultCode == RESULT_CANCELED) {
                    // User cancelled the image capture
                } else {
                    // Image capture failed, advise user
                }
            }

            if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
                if (resultCode == RESULT_OK)
                    // Video captured and saved to fileUri specified in the Intent
                    Toast.makeText(this, "Video saved to:\n" +
                             data.getData(), Toast.LENGTH_LONG).show();
                } else if (resultCode == RESULT_CANCELED) {
                    // User cancelled the video capture
                } else {
                    // Video capture failed, advise user
                }
            }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;


    }


    private static Uri getOutputMediaFileUri(int type){
          return Uri.fromFile(getOutputMediaFile(type));
    }
    private static File getOutputMediaFile(int type){

        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                  Environment.DIRECTORY_PICTURES), "MyCameraApp");
            if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE){
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
            "IMG_"+ timeStamp + ".jpg");
        } else if(type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
            "VID_"+ timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }

}
yogesh
  • 1
  • 3

1 Answers1

0

If you are dealing with fairly small images and the number of images are less, then it is best to store the images in the database as BLOBS. Otherwise, if you have large number of big images, then its better you save the images in the file system and store their URIs in the database

Please read this link to know more about inserting the image byte[] to the database

Community
  • 1
  • 1
desidigitalnomad
  • 1,443
  • 21
  • 33
  • yes exactly I am trying to save uri in DB but i am getting some error .plz check above code and provide me solution – yogesh Apr 17 '14 at 10:15
  • I can't see any DB insert operations in your code? What exception are you getting? Please add your DB class, insert helper functions and also the logcat output – desidigitalnomad Apr 17 '14 at 10:18