0

Hello friends I am using the following code in my my project.

PERMISSIONS:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

XML FILE:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Camera Test" />
    <ImageView android:id="@+id/camera_image"
        android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>

JAVA FILE:-

package com.demo.camera;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ImageView;

public class CameraTest extends Activity {

    private static final int CAMERA_PIC_REQUEST = 1111;
    private ImageView mImage;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera_test);

        mImage = (ImageView) findViewById(R.id.camera_image);
        //1
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, CAMERA_PIC_REQUEST);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_PIC_REQUEST) {
            //2
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            mImage.setImageBitmap(thumbnail);
            //3
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            //4
            File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
            try {
                file.createNewFile();
                FileOutputStream fo = new FileOutputStream(file);
                //5
                fo.write(bytes.toByteArray());
                fo.close();
                Intent intent=new Intent(this,Second.class);
                startActivity(intent);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

When I am using this code it is working fine for me providing me with a preview of the image also, but When I am trying to navigate to second activity, my app hangs... However, sometimes it quickly navigates to second activity. Please help me with issue.

Also every time an additional file is also being added into the camera(DCIM) folder also, so how do I remove it.

Thanks & Regards, onkar

onkar
  • 85
  • 10
  • In your code, in OnCreate method itself you are starting camera... how you are going to second activity? give some more information... – Braj Aug 09 '12 at 06:13
  • @Basavaraj I have an intent after the file has been closed to navigate. – onkar Aug 09 '12 at 06:16

2 Answers2

1

You should check the resultCode too, otherwise your application could crash if you cancel(e.g if you don't took a picture and click the back button).

Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
  • +1 for the answer... I think because of that only its failing to write file to external storage and hanging. You can check like this requestCode == Constants.CAMERA_PIC_REQUEST && resultCode == Activity.RESULT_OK – Braj Aug 09 '12 at 06:31
  • @Basavaraj It writing, but its writing twice once in the directory where I want the code and the other i DCIM folder also.I also want it to rempove the entry from DCIM folder. Also can you help with snippet where the condition check is to be done ? – onkar Aug 09 '12 at 06:36
  • 1
    the removing of image from DCIM folder is another question. Post it in another thread. – Narendra Pal Aug 09 '12 at 06:37
  • In onActivityResult itself if(requestCode == CAMERA_PIC_REQUEST && resultCode == Activity.RESULT_OK) – Braj Aug 09 '12 at 06:39
  • 1
    try this, in your onActivityResult. if(resultCode != RESULT_CANCELED) if(requestCode == CAMERA_PIC_REQUEST) – Narendra Pal Aug 09 '12 at 06:41
  • @onkar. Can you tell me? What response you getting now? – Narendra Pal Aug 09 '12 at 06:47
  • @nick can you help me with deleting the file in DSCIM folder or shall I post the question once again ? – onkar Aug 09 '12 at 06:50
  • 1
    follow this link.http://stackoverflow.com/a/10191062/1395259 If you not able to understand then ask the question in new thread. – Narendra Pal Aug 09 '12 at 06:56
0

check for the resultcode as well. secondaly the code your are using

try {
                file.createNewFile();
                FileOutputStream fo = new FileOutputStream(file);
                //5
                fo.write(bytes.toByteArray());
                fo.close();
                Intent intent=new Intent(this,Second.class);
                startActivity(intent);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

here intent is started after creating a file from the captured bitmap so it takes time and sometimes if there is exception your activity will not be nevigated to next. you can use progress dialog here to show progress of the task

Chandrashekhar
  • 498
  • 4
  • 19
  • yeah mate I will do that UI part later. Also let me know if there is something I can do for fast navigation ? – onkar Aug 09 '12 at 07:14