0

I'm having a serious problem with one specific android device(cat phone) that won't bring the last photo taken with this code:

 Cursor cursor = getContentResolver().query(
                    Media.EXTERNAL_CONTENT_URI, new
                            String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION},
                    Media.DATE_ADDED, null, "date_added ASC");
            if (cursor != null && cursor.moveToFirst()) {
                do {
                    Uri uri = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));
                    photoPath = uri.toString();
                } while (cursor.moveToNext());
                cursor.close();
            }

This works in several phones, several android versions, but It won't work in that phone(moveToFirst returns false).

The worst part is, that some apps WILL update this DB(for example, the take screen capture app). But If after taking a screenshot I take a photo, the last record of that cursor will be the screenshot.

I've found this workaround:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, _uri));
    } else {
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, _uri));
    }

I put that code just before the cursor, but still won't work.

Any idea?

Edit:

I solved the problem, the issue was this:

Camera activity returning null android

But everyone always give half and answer. What I did was this:

... Creating the intent:

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
outputFileUri = getImageUri();            takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);

...

/**
 * @return
 */
private Uri getImageUri() {
    Uri m_imgUri = null;
    File m_file;
    try {
        SimpleDateFormat m_sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        String m_curentDateandTime = m_sdf.format(new Date());
        String m_imagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + m_curentDateandTime + ".jpg";
        m_file = new File(m_imagePath);
        m_imgUri = Uri.fromFile(m_file);
    } catch (Exception p_e) {
    }
    return m_imgUri;
}

Handling the response:

if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

                String photoPath = outputFileUri.getPath();

}
Community
  • 1
  • 1
Mariano L
  • 1,809
  • 4
  • 29
  • 51

1 Answers1

0

The answer is in my EDIT of the original post.

Mariano L
  • 1,809
  • 4
  • 29
  • 51