Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
I want the image clicked by the camera to be saved in my custom folder "MyCameraApp" and that image to be displayed in the app's listview.

Below is the TakePhoto() code:
    
    /**
     * take a photo
     */
    private void activeTakePhoto() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            String fileName = "temp.jpg";
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, fileName);
            mCapturedImageURI = getContentResolver()
                    .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                            values);
            takePictureIntent
                    .putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }


The TakePhoto() function calls REQUEST_IMAGE_CAPTURE:

    case REQUEST_IMAGE_CAPTURE:
                if (requestCode == REQUEST_IMAGE_CAPTURE &&
                        resultCode == RESULT_OK) {
                    String[] projection = {MediaStore.Images.Media.DATA};
                    Cursor cursor =
                            managedQuery(mCapturedImageURI, projection, null,
                                    null, null);
                    int column_index_data = cursor.getColumnIndexOrThrow(
                            MediaStore.Images.Media.DATA);
                    cursor.moveToFirst();
                    String picturePath = cursor.getString(column_index_data);
                    MyImage image = new MyImage();
                    image.setTitle("Test");
                    image.setDescription(
                            "test take a photo and add it to list view");
                    image.setDatetime(System.currentTimeMillis());
                    image.setPath(picturePath);
                    images.add(image);
                    daOdb.addImage(image);
                    imageAdapter.notifyDataSetChanged();
                    listView.invalidateViews();
                }
        }
Posted

1 solution

Your question certainly doesn't reflect on what have you tried or what exception did you faced.All you need to separate your problem into different solutions.

You can save captured image to a specific folder by this code:
Java
private void createDirectoryAndSaveImage(Bitmap imageToSave, String fileName) {

    File direct = new File(Environment.getExternalStorageDirectory() + "/DirName");

    if (!direct.exists()) {
        File wallpaperDirectory = new File("/sdcard/DirName/");
        wallpaperDirectory.mkdirs();
    }

    File file = new File(new File("/sdcard/DirName/"), fileName);
    if (file.exists()) {
        file.delete();
    }
    try {
        FileOutputStream out = new FileOutputStream(file);
        imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

And to show them into listview you can follow it:
Capturing images from camera and setting into listview in android[^]
 
Share this answer
 
Comments
abhay1722 10-Dec-15 10:26am    
Thanks for the reply.But what I actually want is that the code which you have posted,if that can be implemented in my code given above??
ridoy 10-Dec-15 13:00pm    
Inside your case REQUEST_IMAGE_CAPTURE.
abhay1722 11-Dec-15 11:26am    
Thank you very much@ridoy. But could you please integrate your code in mine coz i am not able to figure out that.Sorry for the trouble but your help will be really appreciated.
ridoy 11-Dec-15 12:35pm    
Not tired it, but logically should be ok. In your onActivityResult(),
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
Bitmap bitmap=(Bitmap)data.getExtras().get("data");
// then pass this bitmap image to createDirectoryAndSaveImage() as parameter.
}
abhay1722 11-Dec-15 13:26pm    
Nope,not working!Its giving a nullpointerexception for that bitmap.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900