Example usage for android.content Intent ACTION_MEDIA_SCANNER_SCAN_FILE

List of usage examples for android.content Intent ACTION_MEDIA_SCANNER_SCAN_FILE

Introduction

In this page you can find the example usage for android.content Intent ACTION_MEDIA_SCANNER_SCAN_FILE.

Prototype

String ACTION_MEDIA_SCANNER_SCAN_FILE

To view the source code for android.content Intent ACTION_MEDIA_SCANNER_SCAN_FILE.

Click Source Link

Document

Broadcast Action: Request the media scanner to scan a file and add it to the media database.

Usage

From source file:com.amaze.filemanager.utils.Futils.java

public 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  va2  s .  c  o m*/
                    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:com.anhubo.anhubo.ui.activity.unitDetial.UploadingActivity1.java

/**
 * ?/*from   w w w  .  ja va  2  s. co m*/
 */
private void startCameraCrop() {
    // ??
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(imageUri, "image/*");
    intent.putExtra("scale", true);
    //
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    //?
    intent.putExtra("outputX", 1080);
    intent.putExtra("outputY", 720);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    //
    Intent intentBc = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    intentBc.setData(imageUri);
    this.sendBroadcast(intentBc);
    startActivityForResult(intent, CROP_PHOTO); //??ImageView
}

From source file:com.dazone.crewchat.libGallery.activity.HomeFragmentActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == MediaChooserConstants.CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {

            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, fileUri));
            final AlertDialog alertDialog = MediaChooserConstants.getDialog(HomeFragmentActivity.this).create();
            alertDialog.show();/*w  ww  . j a va  2 s . c  o m*/
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    //Do something after 5000ms
                    String fileUriString = fileUri.toString().replaceFirst("file:///", "/").trim();
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    VideoFragment videoFragment = (VideoFragment) fragmentManager.findFragmentByTag("tab2");
                    //
                    if (videoFragment == null) {
                        VideoFragment newVideoFragment = new VideoFragment();
                        newVideoFragment.addItem(fileUriString);

                    } else {
                        videoFragment.addItem(fileUriString);
                    }
                    alertDialog.cancel();
                }
            }, 5000);

        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the video capture
        } else {
            // Video capture failed, advise user
        }
    } else if (requestCode == MediaChooserConstants.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {

            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, fileUri));

            final AlertDialog alertDialog = MediaChooserConstants.getDialog(HomeFragmentActivity.this).create();
            alertDialog.show();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    //Do something after 5000ms
                    String fileUriString = fileUri.toString().replaceFirst("file:///", "/").trim();
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    ImageFragment imageFragment = (ImageFragment) fragmentManager.findFragmentByTag("tab1");
                    if (imageFragment == null) {
                        ImageFragment newImageFragment = new ImageFragment();
                        newImageFragment.addItem(fileUriString);

                    } else {
                        imageFragment.addItem(fileUriString);
                    }
                    alertDialog.cancel();
                }
            }, 5000);
        }
    }
}

From source file:geert.stef.sm.beheerautokm.Overview.java

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);/*www . j  ava  2  s  .  c  o m*/
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

From source file:org.creativecommons.thelist.misc.AddItemFragment.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case PhotoConstants.PICK_PHOTO_REQUEST:
    case PhotoConstants.TAKE_PHOTO_REQUEST:
        if (resultCode == Activity.RESULT_OK) {
            if (data == null) {
                //Toast.makeText(this,getString(R.string.general_error),Toast.LENGTH_LONG).show();
                Log.d(TAG, "> onActivityResult > data == null");
            } else {
                mMediaUri = data.getData();
            }/* ww  w  .  j  a v a 2s. c o m*/
            Log.i(TAG, "Media URI:" + mMediaUri);

            //Add photo to the Gallery (listen for broadcast and let gallery take action)
            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            mediaScanIntent.setData(mMediaUri);
            getActivity().sendBroadcast(mediaScanIntent);

            Picasso.with(mContext).load(mMediaUri).into(mAddImage);
            mPhotoAdded = true;
        } //RESULT OK
        else if (resultCode != Activity.RESULT_CANCELED) { //result other than ok or cancelled
            //Toast.makeText(this, R.string.general_error, Toast.LENGTH_SHORT).show();
            Log.d(TAG, "> onActivityResult > resultCode != canceled");
        }
        break;
    } //switch
}

From source file:info.papdt.blacklight.support.Utility.java

public static void notifyScanPhotos(Context context, String path) {
    Intent i = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri uri = Uri.fromFile(new File(path));
    i.setData(uri);//from w  w  w  .java2s. c om
    context.sendBroadcast(i);
}

From source file:me.zhang.bingo.Utility.java

/**
 * Invoke the system's media scanner to add your media file to the Media Provider's database,
 * making it available in the Android Gallery application and to other apps.
 *
 * @param context      Context to send broadcast.
 * @param mediaFileUri Your media file./*from ww  w .j  a  va 2  s . c  o m*/
 */
@SuppressWarnings("WeakerAccess")
public static void notifyMediaScannerToAddYourFile(@NonNull Context context, @Nullable Uri mediaFileUri) {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    mediaScanIntent.setData(mediaFileUri);
    context.sendBroadcast(mediaScanIntent);
}

From source file:com.dazone.crewchat.libGallery.activity.BucketHomeFragmentActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {

        if (requestCode == MediaChooserConstants.BUCKET_SELECT_IMAGE_CODE) {
            addMedia(mSelectedImage, data.getStringArrayListExtra("list"));

        } else if (requestCode == MediaChooserConstants.BUCKET_SELECT_VIDEO_CODE) {
            addMedia(mSelectedVideo, data.getStringArrayListExtra("list"));

        } else if (requestCode == MediaChooserConstants.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {

            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, fileUri));
            final AlertDialog alertDialog = MediaChooserConstants.getDialog(BucketHomeFragmentActivity.this)
                    .create();// ww w .  jav a2  s  . com
            alertDialog.show();

            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    //Do something after 2000ms
                    String fileUriString = fileUri.toString().replaceFirst("file:///", "/").trim();
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    BucketImageFragment bucketImageFragment = (BucketImageFragment) fragmentManager
                            .findFragmentByTag("tab1");
                    if (bucketImageFragment != null) {
                        bucketImageFragment.getAdapter().addLatestEntry(fileUriString);
                        bucketImageFragment.getAdapter().notifyDataSetChanged();
                    }
                    alertDialog.dismiss();
                }
            }, 5000);

        } else if (requestCode == MediaChooserConstants.CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {

            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, fileUri));

            final AlertDialog alertDialog = MediaChooserConstants.getDialog(BucketHomeFragmentActivity.this)
                    .create();
            alertDialog.show();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    //Do something after 2000ms
                    String fileUriString = fileUri.toString().replaceFirst("file:///", "/").trim();
                    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
                    BucketVideoFragment bucketVideoFragment = (BucketVideoFragment) fragmentManager
                            .findFragmentByTag("tab2");
                    if (bucketVideoFragment != null) {
                        bucketVideoFragment.getAdapter().addLatestEntry(fileUriString);
                        bucketVideoFragment.getAdapter().notifyDataSetChanged();

                    }
                    alertDialog.dismiss();
                }
            }, 5000);
        }
    }
}

From source file:com.miqtech.master.client.ui.PersonalHomePhotoActivity.java

public static void saveImageToGallery(Context context, Bitmap bmp) {
    // ?//from   w  w  w  .  java  2 s. co  m
    File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");
    if (!appDir.exists()) {
        appDir.mkdir();
    }
    String fileName = System.currentTimeMillis() + ".jpg";
    File file = new File(appDir, fileName);
    try {
        FileOutputStream fos = new FileOutputStream(file);
        bmp.compress(CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // ?
    try {
        MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName,
                null);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    // ?
    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + fileName)));

}

From source file:com.lcl6.cn.imagepickerl.AndroidImagePicker.java

/**
 * scan the photo so that the gallery can read it
 * @param ctx/*from  w w  w . j a va  2 s  .com*/
 * @param path
 */
public static void galleryAddPic(Context ctx, String path) {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(path);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    ctx.sendBroadcast(mediaScanIntent);
    Log.i(TAG, "=====MediaScan:" + path);
}