0

I am going on with my surfaceview camera activty in that i want to get the MimeTye file from Uri of local path while calling the gallery can get the MimeType from Content Uri but while loading from camera can't get the MimeType for that tried to convert the localpath from file to Content Uri for that tried with below:

 public static Uri getImageContentUri(Context context, File imageFile) {
        String filePath = imageFile.getAbsolutePath();
        Cursor cursor = context.getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[] { MediaStore.Images.Media._ID },
                MediaStore.Images.Media.DATA + "=? ",
                new String[] { filePath }, null);
        if (cursor != null && cursor.moveToFirst()) {
            int id = cursor.getInt(cursor
                    .getColumnIndex(MediaStore.MediaColumns._ID));
          //  Uri baseUri = Uri.parse(stringUri);
            return Uri.withAppendedPath(uploadImageURI, "" + id);
        } else {
            if (imageFile.exists()) {
                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.DATA, filePath);
                return context.getContentResolver().insert(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            } else {
                return null;
            }
        }
    }

follwed the link :http://android-er.blogspot.in/2011/04/convert-uri-to-real-path-format.html but the issue didn't fixed.

How to solve this is there any other solution to fix the issue.Is there please help me friends

Manoj
  • 3,947
  • 9
  • 46
  • 84
  • you have to read http://developer.android.com/reference/android/provider/MediaStore.Images.Media.html#CONTENT_TYPE – Zar E Ahmer Mar 06 '15 at 10:40

1 Answers1

1

if you have URI then you can get mimetype like

Uri uri = Uri.fromFile(file);
ContentResolver cR = context.getContentResolver();
String mime = cR.getType(uri);

or try

// url = file path or whatever suitable URL you want.

public static String getMimeType(String url)
{
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extension);
    }
    return type;
}

For more details see these Answers

Community
  • 1
  • 1
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300