Rufen Sie uns an: Deutschland +49 30 23320715 - Österreich +43 1 3059571 | E-Mail: sales@creativeworkline.com
0

Android – Getting file from camera intent on HTC

Posted September 16th, 2011 in Blog by creative workline

This is probably one of the “darkest chapters” of programming for Android. Even such a simple task as opening the built-in camera, capturing an image and returning it to the app can become a challenge if the behaviour differs so much on various devices and OS versions. In connection with this there seems to be an issue in the Android platform that has been unresolved for nearly three years now.

What we found out (thanks to this and other articles) that on certain HTC devices (in our case a Legend with 2.2), you need to first check if the file path that your pass to the intent exists. Have a look at this example code:

private File getCameraImageFile() {
    String storageState = Environment.getExternalStorageState();
    if (storageState.equals(Environment.MEDIA_MOUNTED)) {
        String path = Environment.getExternalStorageDirectory() +
            "" + File.separatorChar + "my_app_name/my_picture.jpg";
        File photoFile = new File(path);
        try {
            if (!photoFile.exists()) {
                photoFile.getParentFile().mkdirs();
                photoFile.createNewFile();
            }
        } catch (IOException e) {
            Log.e(TAG, "Could not create file.", e);
        }
        Log.i(TAG, "Got a file! " + path);
        return photoFile;
    } else {
        Log.i(TAG, "External storage (SD card) is required. Current state: "
            + storageState);
        // show some error dialog here
        return null;
    }
}

If you leave away the mkdirs() and createNewFile() section the HTC’s camera activty will store the image in the media folder (/mnt/sdcard/DCIM/…) but you won’t get the file path in the intent at onActivityResult(). The statement photoFile.createNewFile() will throw an exception if you don’t add this to your AndroidManifest.xml:

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

You can then start an intent like this:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file);
cameraIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(cameraIntent, ACT_REQUEST_CAMERA);

After the camera activity has returned you can read in the file, send it to a server, or do whatever you want to do with it.

VN:F [1.9.22_1171]
Rating: 4.0/5 (1 vote cast)
Android - Getting file from camera intent on HTC, 4.0 out of 5 based on 1 rating

Leave a Reply





Share:  
Mobile App Entwicklung und App Programmierung in Deutschland und Österreich. © App Agentur creative workline GmbH 2016
  App Entwicklung Berlin App Entwicklung Köln App Entwicklung München App Entwicklung Frankfurt App Entwicklung Hamburg App Entwicklung Hannover App Entwicklung Bremen App Entwicklung Düsseldorf App Entwicklung Deutschland App Entwicklung Wien App Entwicklung Österreich In Berlin App erstellen lassen
  App Programmierung Berlin App Programmierung Köln App Programmierung München App Programmierung Hamburg App Programmierung Hannover App Programmierung Bremen App Programmierung Deutschland App Programmierung Wien App Programmierung Österreich