Example usage for android.media MediaScannerConnection scanFile

List of usage examples for android.media MediaScannerConnection scanFile

Introduction

In this page you can find the example usage for android.media MediaScannerConnection scanFile.

Prototype

public static void scanFile(Context context, String[] paths, String[] mimeTypes,
        OnScanCompletedListener callback) 

Source Link

Document

Convenience for constructing a MediaScannerConnection , calling #connect on it, and calling #scanFile with the given path and mimeType when the connection is established.

Usage

From source file:Main.java

public static void scanFile(String path, Context c) {
    System.out.println(path + " " + Build.VERSION.SDK_INT);
    if (Build.VERSION.SDK_INT >= 19) {
        MediaScannerConnection.scanFile(c, new String[] { path }, null,
                new MediaScannerConnection.OnScanCompletedListener() {

                    @Override//from w  w  w. j a va  2  s .  c om
                    public void onScanCompleted(String path, Uri uri) {

                    }
                });
    } else {
        Uri contentUri = Uri.fromFile(new File(path));
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, contentUri);
        c.sendBroadcast(mediaScanIntent);
    }
}

From source file:Main.java

static void registerMediaFile(String file) {
    MediaScannerConnection.scanFile(m_activity, new String[] { file }, null, null);
}

From source file:Main.java

public static boolean installRingtone(final Context context, int resid, final String toneName) {

    String exStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
    String filename = toneName + ".mp3";
    File fileAlarms = new File(exStoragePath, "/Notifications");
    final File fileTone = new File(fileAlarms, filename);

    if (fileTone.exists())
        return false;

    boolean exists = fileAlarms.exists();
    if (!exists) {
        fileAlarms.mkdirs();/*from w w  w .j  a v  a 2s .c  o m*/
    }

    if (fileTone.exists())
        return false;

    byte[] buffer = null;
    InputStream fIn = context.getResources().openRawResource(resid);
    int size = 0;

    try {
        size = fIn.available();
        buffer = new byte[size];
        fIn.read(buffer);
        fIn.close();
    } catch (IOException e) {
        return false;
    }

    FileOutputStream save;
    try {
        save = new FileOutputStream(fileTone);
        save.write(buffer);
        save.flush();
        save.close();
    } catch (FileNotFoundException e) {
        return false;
    } catch (IOException e) {
        return false;
    }

    MediaScannerConnection.scanFile(context, new String[] { fileTone.getAbsolutePath() }, null,
            new MediaScannerConnection.OnScanCompletedListener() {

                @Override
                public void onScanCompleted(String path, Uri uriTone) {

                    ContentValues values = new ContentValues();
                    values.put(MediaStore.MediaColumns.DATA, fileTone.getAbsolutePath());
                    values.put(MediaStore.MediaColumns.TITLE, toneName);
                    values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/mp3");
                    values.put(MediaStore.Audio.Media.ARTIST, "zom");

                    //new
                    values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
                    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
                    values.put(MediaStore.Audio.Media.IS_ALARM, true);
                    values.put(MediaStore.Audio.Media.IS_MUSIC, false);

                    // Insert it into the database
                    Uri newUri = context.getContentResolver().insert(uriTone, values);

                    //                RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);

                    //   Settings.System.putString(context.getContentResolver(),
                    //         Settings.System.RINGTONE, uri.toString());

                }
            });

    return true;
}

From source file:com.frostwire.android.MediaScanner.java

private static void scanFiles(final Context context, List<String> paths, int retries) {
    if (paths.size() == 0) {
        return;//w  w w.j  a  va  2s  .c o m
    }

    LOG.info("About to scan files n: " + paths.size() + ", retries: " + retries);

    final LinkedList<String> failedPaths = new LinkedList<>();

    final CountDownLatch finishSignal = new CountDownLatch(paths.size());

    MediaScannerConnection.scanFile(context, paths.toArray(new String[0]), null, (path, uri) -> {
        try {
            boolean success = true;
            if (uri == null) {
                success = false;
                failedPaths.add(path);
            } else {
                // verify the stored size four faulty scan
                long size = getSize(context, uri);
                if (size == 0) {
                    LOG.warn("Scan returned an uri but stored size is 0, path: " + path + ", uri:" + uri);
                    success = false;
                    failedPaths.add(path);
                }
            }
            if (!success) {
                LOG.info("Scan failed for path: " + path + ", uri: " + uri);
            }
        } finally {
            finishSignal.countDown();
        }
    });

    try {
        finishSignal.await(10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        // ignore
    }

    if (failedPaths.size() > 0 && retries > 0) {
        // didn't want to do this, but there is a serious timing issue with the SD
        // and storage in general
        SystemClock.sleep(2000);
        scanFiles(context, failedPaths, retries - 1);
    }
}

From source file:com.dnielfe.manager.utils.SimpleUtils.java

public static void requestMediaScanner(@NotNull final Context context, @NotNull final File... files) {
    final String[] paths = new String[files.length];
    int i = 0;/*from   w  ww .j av  a2  s.  c  o m*/
    for (final File file : files) {
        paths[i] = file.getPath();
        i++;
    }
    MediaScannerConnection.scanFile(context, paths, null, null);
}

From source file:Main.java

/**
 * Notifies the MediaScanners after Downloading the GhostMySelfie, so it
 * is immediately available to the user.
 *//*from  w w  w  .  j  av  a 2s . c o m*/
public static void notifyMediaScanners(Context context, File ghostmyselfieFile) {
    // Tell the media scanner about the new file so that it is
    // immediately available to the user.
    MediaScannerConnection.scanFile(context, new String[] { ghostmyselfieFile.toString() }, null,
            new MediaScannerConnection.OnScanCompletedListener() {
                public void onScanCompleted(String path, Uri uri) {
                }
            });
}

From source file:com.google.android.apps.forscience.whistlepunk.PictureUtils.java

/**
 * Scan the file when adding or deleting so that media scanner aware apps like Gallery apps can
 * properly index the file.//  w w w  .  ja v a  2s. c o m
 */
public static void scanFile(String photoPath, Context context) {
    MediaScannerConnection.scanFile(context, new String[] { photoPath }, null,
            new MediaScannerConnection.OnScanCompletedListener() {
                @Override
                public void onScanCompleted(String path, Uri uri) {
                    Log.i(TAG, "Scanned " + path + ":");
                    Log.i(TAG, "-> uri=" + uri);
                }
            });
}

From source file:com.wordpress.priyankvex.onetouch.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getFromSdcard();//  w ww  .  j  a v  a2s .co m
    for (String path : f) {
        MediaScannerConnection.scanFile(this, new String[] { path }, new String[] { "image/png" }, null);
    }

    tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    pager = (ViewPager) findViewById(R.id.pager);
    adapter = new MyPagerAdapter(getSupportFragmentManager());

    pager.setAdapter(adapter);

    final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4,
            getResources().getDisplayMetrics());
    pager.setPageMargin(pageMargin);

    tabs.setViewPager(pager);

    changeColor(currentColor);

    SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
    if (prefs.getString("reg_id", null) == null) {
        Intent i = new Intent(MainActivity.this, RegistrationActivity.class);
        startActivity(i);
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        finish();
    }
}

From source file:com.nuvolect.securesuite.util.OmniZip.java

private static void zipSubDirectory(Context ctx, String basePath, OmniFile dir, ZipOutputStream zos)
        throws IOException {

    LogUtil.log(LogUtil.LogType.OMNI_ZIP, "zipSubDirectory : " + dir.getPath());

    OmniFile[] files = dir.listFiles();//from   ww  w  .java 2s . com

    for (OmniFile file : files) {

        if (file.isDirectory()) {
            String path = basePath + file.getName() + File.separator;
            zos.putNextEntry(new ZipEntry(path));
            zipSubDirectory(ctx, path, file, zos);
            zos.closeEntry();
        } else {
            if (file.isStd())// don't scan crypto volume
                MediaScannerConnection.scanFile(ctx, new String[] { file.getAbsolutePath() }, null, null);

            zipFile(basePath, file, zos);
        }
    }
}

From source file:org.mariotaku.twidere.util.SaveFileTask.java

public static File saveFile(final Context context, final File source, final String mimeType,
        final File destination) {
    if (context == null && source == null)
        return null;
    Source ioSrc = null;/*from  ww  w .  ja v a2  s .c o m*/
    BufferedSink sink = null;
    try {
        final String name = source.getName();
        if (isEmpty(name))
            return null;
        final MimeTypeMap map = MimeTypeMap.getSingleton();
        final String extension = map.getExtensionFromMimeType(mimeType);
        if (extension == null)
            return null;
        final String nameToSave = getFileNameWithExtension(name, extension);
        if (!destination.isDirectory() && !destination.mkdirs())
            return null;
        final File saveFile = new File(destination, nameToSave);
        ioSrc = Okio.source(source);
        sink = Okio.buffer(Okio.sink(saveFile));
        sink.writeAll(ioSrc);
        sink.flush();
        if (mimeType != null) {
            MediaScannerConnection.scanFile(context, new String[] { saveFile.getPath() },
                    new String[] { mimeType }, null);
        }
        return saveFile;
    } catch (final IOException e) {
        final int errno = Utils.getErrorNo(e.getCause());
        Log.w(LOGTAG, "Failed to save file", e);
        return null;
    } finally {
        Utils.closeSilently(sink);
        Utils.closeSilently(ioSrc);
    }
}