1

I am developing a simple gallery app in which I search for folders which contains images and then show images of that particular folder. First I am using Environment.getExternalStorageDirectory() and then recursively search for folders having images. It is working fine simulator and devices. When my client installed app on Nexus 4, nothing is being loaded. I have seen some posts which says that Nexus series don't have any external SD slot. I don't have Nexus 4 for debugging and client is also non-technical. He can troubleshoot on his own to find the cause of problem. Can anybody help in this regard? I think the problem is in Environment.getExternalStorageDirectory() call which is not applicable in Nexus. Any idea how I can tackle this issue? Here is the code snipped I am using:

    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File cacheDir = null;
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
            cacheDir=new File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath(), "MyImages");
        }
        else {
            cacheDir = getCacheDir();
        }
        if(!cacheDir.exists()) {
            cacheDir.mkdir();
        }
//      File storageDir = Environment.getExternalStoragePublicDirectory(
//              Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                cacheDir      /* directory */
                );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = "" + image.getAbsolutePath();
        return image;
    }

    public void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                ex.printStackTrace();
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photoFile));
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }

And here the permissions I have added manifest:

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

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

<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />

Thanks!

Khawar Raza
  • 15,870
  • 24
  • 70
  • 127

1 Answers1

0

I think you should give a try to the MediaStore.Images component. Load the images using a cursor. See documentation: http://developer.android.com/reference/android/provider/MediaStore.html

Thalis Vilela
  • 335
  • 1
  • 10
  • Yes, I had to change my approach to Media query and it worked fine. But now I am facing another issue. The app has feature of capturing photo using camera. I am trying to create file and then passing the file info to camera intent. Again working on other devices but have issue on Nexus 4. App is unable to create file. I tried Environment. getexternalstoragedirextory and getCacheDir and both failed. Any idea how to create file for camera intent in Nexus series...??? – Khawar Raza Jul 18 '14 at 17:50
  • Double check if you are requesting the right permissions in the manifest, and give us some LogCat doodoo... :) – Thalis Vilela Jul 18 '14 at 20:06
  • I have updated my question. Please have a look on it. Also I don't have Nexus 4 phone. So I cannot provide LogCat output here. Thanks – Khawar Raza Jul 19 '14 at 11:16
  • yeah, actually I just had to if(!image.exists()) {image.createNewFile();} after image object creation. – Khawar Raza Aug 11 '14 at 19:00