Aleksandar Zoric
Aleksandar Zoric

Reputation: 1483

How to check/set mime type in Android

Quick overview; I am building a Android application for file sharing, just began really but a little stuck on something. I want to enable the user to share multiple different file formats e.g. images, documents, videos etc. Now I got it all that working but only one at a time as show below. I have to change the string variable 'type' to certain mime type in order to open that file. I have the URI of the file that is selected if that helps. I have Toasts in there just for testing purposes. Basically what I wish to achieve is that the application will check what file format is selected and open the apporiate third party app to preview that file when the preview button is clicked e.g. .JPG file will open pictures/gallery.

I believe code needs to be added to the 'previewBtn' in the onClick method but could be wrong.

Is there a way that the application can check what file is selected, and then set 'type' to be equal to that?

public class ShareFilesActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "ShareFilesActivity";
TextView mTextView = null;
String mimeType = null;

private static final int REQUEST_CODE = 6384; // onActivityResult request
// code
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_share_files);

    Button chooseFile;
    chooseFile = (Button) findViewById(R.id.chooseFileBtn);
    chooseFile.setOnClickListener(this);

    Button previewBtn;
    previewBtn = (Button) findViewById(R.id.previewBtn);
    previewBtn.setOnClickListener(this);


}

@Override
public void onClick(View v) {
    switch (v.getId()) {

        case R.id.chooseFileBtn:
            showChooser();
            break;

        case R.id.previewBtn:
            String input = mTextView.getText().toString();
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction(Intent.ACTION_VIEW);
            String type = "image/*";
            Toast.makeText(ShareFilesActivity.this, "Type3: " + type, Toast.LENGTH_LONG).show();

            intent.setDataAndType(Uri.parse(input),type);
            startActivity(intent);
            Toast.makeText(ShareFilesActivity.this, input, Toast.LENGTH_LONG).show();
            Toast.makeText(ShareFilesActivity.this, "Type1: " + mimeType, Toast.LENGTH_LONG).show();



            break;

        default:
            break;
    }

}

private void showChooser() {
    // Use the GET_CONTENT intent from the utility class
    Intent target = FileUtils.createGetContentIntent();
    // Create the chooser Intent
    Intent intent = Intent.createChooser(
            target, getString(R.string.chooser_title));
    try {
        startActivityForResult(intent, REQUEST_CODE);
    } catch (ActivityNotFoundException e) {
        // The reason for the existence of aFileChooser
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_CODE:
                // If the file selection was successful
                if (resultCode == RESULT_OK) {
                    if (data != null) {
                        // Get the URI of the selected file
                        final Uri uri = data.getData();
                        mimeType = getContentResolver().getType(uri);
                        Toast.makeText(ShareFilesActivity.this, "Type2: " + mimeType, Toast.LENGTH_LONG).show();
                        Log.i(TAG, "Uri = " + uri.toString());
                        mTextView = (TextView) findViewById(R.id.fileNameTextView);
                        mTextView.setText(uri.toString());
                        mimeType = getContentResolver().getType(uri);

                        try {
                            // Get the file path from the URI
                            final String path = FileUtils.getPath(this, uri);
                        } catch (Exception e) {
                            Log.e("ShareFilesActivity", "File Select Error", e);
                        }
                    }
                }

                break;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

}

Upvotes: 0

Views: 4310

Answers (1)

Rakshit Nawani
Rakshit Nawani

Reputation: 2604

To check mime type you can use the below function and change the type of your own use

      public boolean isVideoFile(String path) {
        String mimeType = URLConnection.guessContentTypeFromName(path);
        System.out.println(mimeType);
        return mimeType != null && mimeType.indexOf("video") == 0;

  }

Will return true if the type is video on my case

Hope this helps.

Upvotes: 3

Related Questions