0

am new to android. am going create a android app. I want to create a album folder into my app and save camera photos into albums folder. please suggest me how.

my code is:

public class CameraPhotoCapture extends Activity {

final static int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1;


Uri imageUri                      = null;
static TextView imageDetails      = null;
public  static ImageView showImg  = null;
CameraPhotoCapture CameraActivity = null;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera_photo_capture);
    CameraActivity = this;

    imageDetails = (TextView) findViewById(R.id.imageDetails);

    showImg = (ImageView) findViewById(R.id.showImg);

    final Button photo = (Button) findViewById(R.id.photo);



    photo.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            /***** Define the file-name to save photo taken by Camera activity *******/
            String fileName = "Camera_Example.jpg";

            // Create parameters for Intent with filename

            ContentValues values = new ContentValues();

            values.put(MediaStore.Images.Media.TITLE, fileName);

            values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");

            /****** imageUri is the current activity attribute, define and save it for later usage  *****/
            imageUri = getContentResolver().insert(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

            /******   EXTERNAL_CONTENT_URI : style URI for the "primary" external storage volume. ******/


            /******  Standard Intent action that can be sent to have the camera application capture an image and return it. ******/ 

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

             intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

             intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

        }   

    });
}

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {

            if (resultCode == RESULT_OK) {

                String imageId = convertImageUriToFile(imageUri,CameraActivity);

                new LoadImagesFromSDCard().execute(""+imageId);


            } else if (resultCode == RESULT_CANCELED) {

                Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
            } else {

                Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
            }
        }
    }


 public static String convertImageUriToFile (Uri imageUri, Activity activity)  {
        Cursor cursor = null;
        int imageID = 0;

        try {
            /*********** Which columns values want to get *******/
            String [] proj={
                             MediaStore.Images.Media.DATA, 
                             MediaStore.Images.Media._ID,
                             MediaStore.Images.Thumbnails._ID, 
                             MediaStore.Images.ImageColumns.ORIENTATION
                           };

            cursor = activity.managedQuery( 

                            imageUri,   // Get data for specific image URI
                            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 columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
            int columnIndexThumb = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
            int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            //int orientation_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION);

            int size = cursor.getCount();

            /*******  If size is 0, there are no images on the SD Card. *****/

            if (size == 0) {
                imageDetails.setText("No Image");
            }
            else
            {

                int thumbID = 0;
                if (cursor.moveToFirst()) {

                    /**************** Captured image details ************/

                    /*****  Used to show image on view in LoadImagesFromSDCard class ******/
                    imageID = cursor.getInt(columnIndex);

                    thumbID   = cursor.getInt(columnIndexThumb);

                    String Path = cursor.getString(file_ColumnIndex);

                    //String orientation =  cursor.getString(orientation_ColumnIndex);

                    String CapturedImageDetails = " CapturedImageDetails : \n\n"
                                                      +" ImageID :"+imageID+"\n"
                                                      +" ThumbID :"+thumbID+"\n"
                                                      +" Path :"+Path+"\n";

                    // Show Captured Image detail on view
                    imageDetails.setText(CapturedImageDetails);

                }
            }    
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }

        return ""+imageID;
    }


     /**
     * Async task for loading the images from the SD card. 
     * 
     * @author Android Example
     *
     */
    // Class with extends AsyncTask class
    public class LoadImagesFromSDCard  extends AsyncTask<String, Void, Void> {

        private ProgressDialog Dialog = new ProgressDialog(CameraPhotoCapture.this);

        Bitmap mBitmap;

        protected void onPreExecute() {
            /****** NOTE: You can call UI Element here. *****/

            //UI Element
            Dialog.setMessage("Loading image from Sdcard..");
            Dialog.show();
        }

        // Call after onPreExecute method
        protected Void doInBackground(String... urls) {

            Bitmap bitmap = null;
            Bitmap newBitmap = null;
            Uri uri = null;       


                try {

                    /**  Uri.withAppendedPath Method Description
                    * Parameters
                    *    baseUri  Uri to append path segment to 
                    *    pathSegment  encoded path segment to append 
                    * Returns
                    *    a new Uri based on baseUri with the given segment appended to the path
                    */

                    uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + urls[0]);

                    /**************  Decode an input stream into a bitmap. *********/
                    bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));

                    if (bitmap != null) {

                        /********* Creates a new bitmap, scaled from an existing bitmap. ***********/

                        newBitmap = Bitmap.createScaledBitmap(bitmap, 170, 170, true); 

                        bitmap.recycle();

                        if (newBitmap != null) {

                            mBitmap = newBitmap;
                        }
                    }
                } catch (IOException e) {
                    //Error fetching image, try to recover

                    /********* Cancel execution of this task. **********/
                    cancel(true);
                }

            return null;
        }

        protected void onPostExecute(Void unused) {

            // NOTE: You can call UI Element here.

            // Close progress dialog
            Dialog.dismiss();

            if(mBitmap != null)
              showImg.setImageBitmap(mBitmap);

        }

    }

}

Ramakrishna
  • 26
  • 1
  • 6
  • The question is not clear. Do You have any problems with creating of folder? Or with saving/copying photos into it? If so, then please share the code. – sandrstar Oct 29 '13 at 08:43
  • yes i have both problems. In creating a folder and also saving camera photos into it.And my code is show in question – Ramakrishna Oct 29 '13 at 09:03

1 Answers1

0

try this one.

String path = Environment.getExternalStorageDirectory() + "/appsalbum/example.jpg";
File file = new File(path);
Uri outputFileUri = Uri.fromFile( file );
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
startActivityForResult( intent, CAPTURE_IMAGE );
William Reed
  • 1,717
  • 1
  • 17
  • 30
blitzen12
  • 1,350
  • 3
  • 24
  • 33
  • By using this code, photo directly stored in my app? – Ramakrishna Oct 29 '13 at 07:37
  • photo will be stored to the albumfolder of your app. just like the default camera app for android which is stored in DCIM/Camera – blitzen12 Oct 29 '13 at 08:02
  • Environment.getExternalStorageDirectory() by using this method the photo directly stored in the gallery ?. We want to save the photo in my app(album folder). if we want to specify any path to store in my app itself. – Ramakrishna Oct 29 '13 at 08:42
  • Environment.getExternalStorageDirectory returns the device external storage. "appsalbum" is your app specified folder. – blitzen12 Oct 29 '13 at 08:51
  • Thank you @blitzen. but how can i create appsalbum folder in my app – Ramakrishna Oct 29 '13 at 09:05
  • see this http://stackoverflow.com/questions/8402378/how-to-save-image-captured-with-camera-in-specific-folder – blitzen12 Oct 29 '13 at 09:11