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:nf.frex.android.FrexActivity.java

private void updateMediaContentProvider(File imageFile) {
    MediaScannerConnection.scanFile(this, new String[] { imageFile.getPath() }, new String[] { "image/*" },
            new MediaScannerConnection.OnScanCompletedListener() {
                @Override/*from   w  w w  . j  ava  2s .com*/
                public void onScanCompleted(String path, Uri uri) {

                }
            });
}

From source file:info.guardianproject.otr.app.im.app.NewChatActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent resultIntent) {
    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_SEND_IMAGE || requestCode == REQUEST_SEND_FILE
                || requestCode == REQUEST_SEND_AUDIO) {
            Uri uri = resultIntent.getData();
            if (uri == null) {
                return;
            }//from  w  w  w  . j a  v  a2 s. co  m
            boolean deleteAudioFile = (requestCode == REQUEST_SEND_AUDIO);
            handleSendDelete(uri, null, deleteAudioFile, false);
        } else if (requestCode == REQUEST_TAKE_PICTURE) {
            File file = new File(getRealPathFromURI(mLastPhoto));
            final Handler handler = new Handler();
            MediaScannerConnection.scanFile(this, new String[] { file.toString() }, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        @Override
                        public void onScanCompleted(String path, final Uri uri) {

                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    handleSendDelete(mLastPhoto, "image/*", true, true);
                                }
                            });
                        }
                    });
        } else if (requestCode == REQUEST_TAKE_PICTURE_SECURE) {
            String filename = resultIntent.getStringExtra(SecureCameraActivity.FILENAME);
            String mimeType = resultIntent.getStringExtra(SecureCameraActivity.MIMETYPE);
            Uri uri = Uri.parse("file:" + filename);
            handleSend(uri, mimeType);
        } else if (requestCode == REQUEST_SETTINGS) {

            try {
                mApp.getRemoteImService().updateStateFromSettings();
            } catch (Exception e) {
                Log.e(ImApp.LOG_TAG, "unable to update service settings", e);
            }

            finish();
            Intent intent = new Intent(getApplicationContext(), NewChatActivity.class);
            startActivity(intent);

        } else if (requestCode == REQUEST_PICK_CONTACTS || requestCode == REQUEST_ADD_CONTACT) {

            String username = resultIntent.getStringExtra(ContactsPickerActivity.EXTRA_RESULT_USERNAME);
            long providerId = resultIntent.getLongExtra(ContactsPickerActivity.EXTRA_RESULT_PROVIDER, -1);

            //String message = resultIntent.getStringExtra(ContactsPickerActivity.EXTRA_RESULT_MESSAGE);
            try {

                IChatSession chatSession = this.getCurrentChatSession();
                if (chatSession != null && chatSession.isGroupChatSession()) {
                    chatSession.inviteContact(username);
                    showInvitationHasSent(username);
                } else {
                    startChat(providerId, username, Imps.ContactsColumns.TYPE_NORMAL, true, null);
                }
            } catch (RemoteException e) {
                mHandler.showServiceErrorAlert("Error picking contacts");
                Log.d(ImApp.LOG_TAG, "error picking contact", e);
            }
        }

        IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, resultIntent);

        if (scanResult != null) {
            String xmppUri = scanResult.getContents();
            String result = null;
            if (xmppUri.startsWith("xmpp"))
                result = XmppUriHelper.getOtrFingerprint(xmppUri);

            if (getCurrentChatView() != null && result != null)
                getCurrentChatView().verifyScannedFingerprint(result);
            else {
                //add new contact?
            }

        }
    }
}

From source file:com.rks.musicx.misc.utils.Helper.java

/**
 * Delete Track//  w  w  w. j av  a2 s . co m
 *
 * @param name
 * @param path
 * @param context
 */
public void DeleteTrack(RefreshData refreshData, String name, String path, Context context) {
    MaterialDialog.Builder dialog = new MaterialDialog.Builder(context);
    dialog.title(name);
    dialog.content(R.string.delete_music);
    dialog.positiveText(android.R.string.ok);
    dialog.typeface(getFont(context), getFont(context));
    dialog.onPositive(new MaterialDialog.SingleButtonCallback() {
        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            if (path != null) {
                File file = new File(path);
                if (file.exists()) {
                    if (file.delete()) {
                        Log.e("-->", "file Deleted :" + path);
                        MediaScannerConnection.scanFile(context, new String[] { file.getAbsolutePath() },
                                new String[] { "audio/*" },
                                new MediaScannerConnection.MediaScannerConnectionClient() {
                                    @Override
                                    public void onMediaScannerConnected() {

                                    }

                                    @Override
                                    public void onScanCompleted(String s, Uri uri) {
                                        refreshData.refresh();
                                    }
                                });
                        Toast.makeText(context, "Song deleted", Toast.LENGTH_SHORT).show();
                    } else {
                        Log.e("-->", "file not Deleted :" + name);
                        refreshData.refresh();
                        Toast.makeText(context, "Failed to delete song", Toast.LENGTH_SHORT).show();
                    }

                } else {
                    refreshData.refresh();
                }
            } else {
                Log.d("Helper", "Path not found");
            }

        }
    });
    dialog.negativeText(R.string.cancel);
    dialog.show();
}

From source file:org.cryptsecure.Utility.java

/**
 * Update media scanner so that the image is shown in the gallery without a
 * reboot of the device.//from w w  w .j  a v  a2  s.co m
 * 
 * @param context
 *            the context
 * @param imagePath
 *            the image path
 */
public static void updateMediaScanner(Context context, String imagePath) {

    MediaScannerConnection.scanFile(context, new String[] { imagePath }, null,
            new MediaScannerConnection.OnScanCompletedListener() {
                public void onScanCompleted(String path, Uri uri) {
                }
            });
}

From source file:com.vegnab.vegnab.MainVNActivity.java

public void copyFile(File src, File dst) throws IOException {
    FileInputStream in = new FileInputStream(src);
    FileOutputStream out = new FileOutputStream(dst);
    FileChannel fromChannel = null, toChannel = null;
    try {/*from  www. j ava 2  s  .  c  om*/
        fromChannel = in.getChannel();
        toChannel = out.getChannel();
        fromChannel.transferTo(0, fromChannel.size(), toChannel);
    } finally {
        if (fromChannel != null)
            fromChannel.close();
        if (toChannel != null)
            toChannel.close();
    }
    in.close();
    out.close();
    // must do following or file is not visible externally
    MediaScannerConnection.scanFile(getApplicationContext(), new String[] { dst.getAbsolutePath() }, null,
            null);
}

From source file:ac.robinson.mediaphone.MediaPhoneActivity.java

protected BackgroundRunnable getMediaLibraryAdderRunnable(final String mediaPath,
        final String outputDirectoryType) {
    return new BackgroundRunnable() {
        @Override//from  ww w .  j av a 2s.c  o  m
        public int getTaskId() {
            return 0;
        }

        @Override
        public boolean getShowDialog() {
            return false;
        }

        @Override
        public void run() {
            if (IOUtilities.externalStorageIsWritable()) {
                File outputDirectory = Environment.getExternalStoragePublicDirectory(outputDirectoryType);
                try {
                    outputDirectory.mkdirs();
                    File mediaFile = new File(mediaPath);
                    // use current time as this happens at creation; newDatedFileName guarantees no collisions
                    File outputFile = IOUtilities.newDatedFileName(outputDirectory,
                            IOUtilities.getFileExtension(mediaFile.getName()));
                    IOUtilities.copyFile(mediaFile, outputFile);
                    MediaScannerConnection.scanFile(MediaPhoneActivity.this,
                            new String[] { outputFile.getAbsolutePath() }, null,
                            new MediaScannerConnection.OnScanCompletedListener() {
                                @Override
                                public void onScanCompleted(String path, Uri uri) {
                                    if (MediaPhone.DEBUG)
                                        Log.d(DebugUtilities.getLogTag(this), "MediaScanner imported " + path);
                                }
                            });
                } catch (IOException e) {
                    if (MediaPhone.DEBUG)
                        Log.d(DebugUtilities.getLogTag(this), "Unable to save media to " + outputDirectory);
                }
            }
        }
    };
}