Hi,

trying to capture image and display it but getting FileNotFound for
following code. Anybody willing to take a look at it?



public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    private static final int CAMERA_PIC_REQUEST = 1337;
    Button btn1;
    ImageView image;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        final File path = new File(
Environment.getExternalStorageDirectory(), getPackageName() );

        Toast.makeText(this, path.toString() , Toast.LENGTH_SHORT).show();


        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(getTempFile(this)) );
        startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);


       image = (ImageView) findViewById(R.id.fotografija);
       //image.setImageBitmap(thumbnail);

       btn1 = (Button) findViewById(R.id.button1);
       btn1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent cameraIntent = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(getTempFile(v.getContext())) );
                startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAMERA_PIC_REQUEST) {
            if(resultCode == RESULT_OK){
                Toast.makeText(this, "Captured! ",
Toast.LENGTH_SHORT).show();

                final File file = getTempFile(this);

                try {
                  Bitmap captureBmp = Media.getBitmap(getContentResolver(),
Uri.fromFile(file) );

                  image.setImageBitmap(captureBmp);


                } catch (FileNotFoundException e) {
                    Toast.makeText(this, "FileNotFoundException",
Toast.LENGTH_SHORT).show();
                  e.printStackTrace();
                } catch (IOException e) {
                    Toast.makeText(this, "IOException",
Toast.LENGTH_SHORT).show();
                  e.printStackTrace();
                }


            }else if(resultCode == RESULT_CANCELED){
                Toast.makeText(this, "User canceled!",
Toast.LENGTH_SHORT).show();

            }
        }

    }


    private File getTempFile(Context context){
          //it will return /sdcard/image.tmp
          final File path = new File(
Environment.getExternalStorageDirectory(), context.getPackageName() );
          if(!path.exists()){
            path.mkdir();
          }
          return new File(path, "image.tmp");
        }
}



--------------------------------------------------------------------------------------------------------------


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android";
      package="com.example.helloandroid"
      android:versionCode="1"
      android:versionName="1.0">

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

    <application android:icon="@drawable/icon"
android:label="@string/app_name" android:debuggable="true">
        <activity android:name=".HelloAndroid"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>



-- 
God is Real, unless declared Integer.
J. Allan Toogood, FORTRAN programmer

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to