Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import android.annotation.TargetApi;
import android.content.Context;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.OnScanCompletedListener;
import android.net.Uri;

import android.os.Build;
import android.util.Log;

public class Main {
    /**
     * Trigger the media scanner to ensure files show up in MTP.
     * @param context a context to use for communication with the media scanner
     * @param scanfile directory or file to scan
     */
    @TargetApi(11)
    private static void triggerMediaScanner(Context context, File scanfile) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
            return; // API 11 - lower versions do not have MTP
        try {
            String path = scanfile.getCanonicalPath();
            Log.i("SavingHelper", "Triggering media scan for " + path);
            MediaScannerConnection.scanFile(context, new String[] { path }, null, new OnScanCompletedListener() {
                @Override
                public void onScanCompleted(String path, Uri uri) {
                    Log.i("SavingHelper", "Media scan completed for " + path + " URI " + uri);
                }
            });
        } catch (Exception e) {
            Log.e("SavingHelper", "Exception when triggering media scanner", e);
        }
    }
}