本文整理汇总了Java中android.media.MediaScannerConnection类的典型用法代码示例。如果您正苦于以下问题:Java MediaScannerConnection类的具体用法?Java MediaScannerConnection怎么用?Java MediaScannerConnection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MediaScannerConnection类属于android.media包,在下文中一共展示了MediaScannerConnection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: scanSavedMediaFile
import android.media.MediaScannerConnection; //导入依赖的package包/类
/**
* Notifies the OS to index the new image, so it shows up in Gallery. Allows optional callback method to notify client when
* the scan is completed, e.g. so it can access the "content" URI that gets assigned.
*/
public static void scanSavedMediaFile(final Context context, final String path, final MediaScannerCallback callback) {
// silly array hack so closure can reference scannerConnection[0] before it's created
final MediaScannerConnection[] scannerConnection = new MediaScannerConnection[1];
try {
MediaScannerConnection.MediaScannerConnectionClient scannerClient = new MediaScannerConnection.MediaScannerConnectionClient() {
public void onMediaScannerConnected() {
scannerConnection[0].scanFile(path, null);
}
public void onScanCompleted(String scanPath, Uri scanURI) {
scannerConnection[0].disconnect();
if (callback != null) {
callback.mediaScannerCompleted(scanPath, scanURI);
}
}
};
scannerConnection[0] = new MediaScannerConnection(context, scannerClient);
scannerConnection[0].connect();
} catch (Exception ignored) {
}
}
示例2: fileScan
import android.media.MediaScannerConnection; //导入依赖的package包/类
/**
* 扫描指定文件夹Android4.4中,则会抛出异常MediaScannerConnection.scanFile可以解决
*/
public static void fileScan(Context context) {
try {
if (Build.VERSION.SDK_INT < 19) {
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
} else {
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
MediaScannerConnection.scanFile(context, new String[]{new File(Environment.getExternalStorageDirectory().toString()).getAbsolutePath()}, null, new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
示例3: addToGalleryAndNotify
import android.media.MediaScannerConnection; //导入依赖的package包/类
public static void addToGalleryAndNotify(Context context, File imageFile, final Promise promise) {
final WritableMap response = new WritableNativeMap();
response.putString("path", Uri.fromFile(imageFile).toString());
// borrowed from react-native CameraRollManager, it finds and returns the 'internal'
// representation of the image uri that was just saved.
// e.g. content://media/external/images/media/123
MediaScannerConnection.scanFile(
context,
new String[]{imageFile.getAbsolutePath()},
null,
new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
promise.resolve(response);
}
});
}
示例4: onFinish
import android.media.MediaScannerConnection; //导入依赖的package包/类
@Override
public void onFinish(DownloadInfo downloadInfo) {
// sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(LqBookConst.DOWN_PATH)));
Log.i(TAG,"onFinish="+downloadInfo.getFileName());
Toast.makeText(getApplicationContext(), getString(R.string.downLoaded) + downloadInfo.getTargetPath(), Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
/**
* 重新扫描数据库
*/
MediaScannerConnection.scanFile(getApplicationContext(),
new String[]{downloadInfo.getTargetPath()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
}
示例5: scanFile
import android.media.MediaScannerConnection; //导入依赖的package包/类
/**
* 扫描指定的文件
*
* @param context
* @param filePath
* @param sListener
*/
public static MediaScannerConnection scanFile(Context context, String[] filePath, String[] mineType,
MediaScannerConnection.OnScanCompletedListener sListener) {
ClientProxy client = new ClientProxy(filePath, mineType, sListener);
try {
MediaScannerConnection connection = new MediaScannerConnection(
context.getApplicationContext(), client);
client.mConnection = connection;
connection.connect();
return connection;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例6: saveFile
import android.media.MediaScannerConnection; //导入依赖的package包/类
@SuppressWarnings("ResultOfMethodCallIgnored")
private boolean saveFile(String fileName) {
try {
File path = getExternalStoragePublicDirectory(getString(R.string.storage_dir));
path.mkdir();
File file = new File(path, fileName);
FileOutputStream stream = new FileOutputStream(file);
stream.write(getXML().getBytes());
stream.flush();
stream.close();
MediaScannerConnection.scanFile(this, new String[] {file.toString()}, null, null);
return true;
} catch (IOException e) {
return false;
}
}
示例7: saveAttachment
import android.media.MediaScannerConnection; //导入依赖的package包/类
private boolean saveAttachment(Context context, MasterSecret masterSecret, Attachment attachment) throws IOException {
String contentType = MediaUtil.getCorrectedMimeType(attachment.contentType);
File mediaFile = constructOutputFile(contentType, attachment.filename, attachment.date);
InputStream inputStream = PartAuthority.getAttachmentStream(context, masterSecret, attachment.uri);
if (inputStream == null) {
return false;
}
OutputStream outputStream = new FileOutputStream(mediaFile);
Util.copy(inputStream, outputStream);
MediaScannerConnection.scanFile(context, new String[]{mediaFile.getAbsolutePath()},
new String[]{contentType}, null);
return true;
}
示例8: saveToSDCard
import android.media.MediaScannerConnection; //导入依赖的package包/类
public static String saveToSDCard(byte[] data,Context context,String path) throws IOException {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String filename = "IMG_" + format.format(date) + ".jpg";
File fileFolder = new File(path);
if (!fileFolder.exists()) {
fileFolder.mkdirs();
}
File jpgFile = new File(fileFolder, filename);
FileOutputStream outputStream = new FileOutputStream(jpgFile); //
//刷新相册
MediaScannerConnection.scanFile(context,
new String[]{jpgFile.getAbsolutePath()}, null, null);
outputStream.write(data);
outputStream.close();
// Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
// Uri uri = Uri.fromFile(new File(Environment
// .getExternalStorageDirectory() + "/DeepbayPicture/" + filename));
// intent.setData(uri);
// mContext.sendBroadcast(intent);
return jpgFile.getAbsolutePath();
}
示例9: onActivityResult
import android.media.MediaScannerConnection; //导入依赖的package包/类
/**
* Check if the captured image is stored successfully
* Then reload data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constants.REQUEST_CODE_CAPTURE) {
if (resultCode == RESULT_OK && currentImagePath != null) {
Uri imageUri = Uri.parse(currentImagePath);
if (imageUri != null) {
MediaScannerConnection.scanFile(this,
new String[]{imageUri.getPath()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
Log.v(TAG, "File " + path + " was scanned successfully: " + uri);
getDataWithPermission();
}
});
}
}
}
}
示例10: download
import android.media.MediaScannerConnection; //导入依赖的package包/类
private void download() {
Observable.just(null)
.compose(bindToLifecycle())
.compose(ensurePermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE))
.filter(granted -> {
if (granted) {
return true;
} else {
Toasty.info(this, getString(R.string.permission_required),
Toast.LENGTH_LONG).show();
return false;
}
})
.flatMap(granted -> ensureDirectory("Moment"))
.map(file -> new File(file, makeFileName()))
.flatMap(file -> file.exists()
? Observable.just(file)
: save(file))
.doOnNext(file -> MediaScannerConnection.scanFile(getApplicationContext(),
new String[]{file.getPath()}, null, null))
.subscribe(file -> {
showTips(getString(R.string.save_path_tips, file.getPath()));
});
}
示例11: backupWallet
import android.media.MediaScannerConnection; //导入依赖的package包/类
private boolean backupWallet(String walletName) {
File backupFolder = new File(getStorageRoot(), "backups");
if (!backupFolder.exists()) {
if (!backupFolder.mkdir()) {
Timber.e("Cannot create backup dir %s", backupFolder.getAbsolutePath());
return false;
}
// make folder visible over USB/MTP
MediaScannerConnection.scanFile(this, new String[]{backupFolder.toString()}, null, null);
}
File walletFile = Helper.getWalletFile(LoginActivity.this, walletName);
File backupFile = new File(backupFolder, walletName);
Timber.d("backup " + walletFile.getAbsolutePath() + " to " + backupFile.getAbsolutePath());
// TODO probably better to copy to a new file and then rename
// then if something fails we have the old backup at least
// or just create a new backup every time and keep n old backups
boolean success = copyWallet(walletFile, backupFile, true, true);
Timber.d("copyWallet is %s", success);
return success;
}
示例12: saveAttachment
import android.media.MediaScannerConnection; //导入依赖的package包/类
private @Nullable File saveAttachment(Context context, MasterSecret masterSecret, Attachment attachment)
throws NoExternalStorageException, IOException
{
String contentType = MediaUtil.getCorrectedMimeType(attachment.contentType);
String fileName = attachment.fileName;
if (fileName == null) fileName = generateOutputFileName(contentType, attachment.date);
fileName = sanitizeOutputFileName(fileName);
File outputDirectory = createOutputDirectoryFromContentType(contentType);
File mediaFile = createOutputFile(outputDirectory, fileName);
InputStream inputStream = PartAuthority.getAttachmentStream(context, masterSecret, attachment.uri);
if (inputStream == null) {
return null;
}
OutputStream outputStream = new FileOutputStream(mediaFile);
Util.copy(inputStream, outputStream);
MediaScannerConnection.scanFile(context, new String[]{mediaFile.getAbsolutePath()},
new String[]{contentType}, null);
return mediaFile.getParentFile();
}
示例13: onPostExecute
import android.media.MediaScannerConnection; //导入依赖的package包/类
@Override
protected void onPostExecute(Boolean b) {
super.onPostExecute(b);
if (b) {
Toast.makeText(context, "Tag Edit Success", Toast.LENGTH_SHORT).show();
mediaScannerConnection = new MediaScannerConnection(getContext(),
new MediaScannerConnection.MediaScannerConnectionClient() {
public void onScanCompleted(String path, Uri uri) {
mediaScannerConnection.disconnect();
}
public void onMediaScannerConnected() {
mediaScannerConnection.scanFile(song.getmSongPath(), "audio/*");
}
});
} else {
Toast.makeText(context, "Tag Edit Failed", Toast.LENGTH_SHORT).show();
}
}
示例14: scanFile
import android.media.MediaScannerConnection; //导入依赖的package包/类
/**
* 扫描文件, 到系统相册/视频文件夹
*/
public static void scanFile(Context context, String filePath) {
File file = new File(filePath);
if (file.exists() && context != null) {
/*需要android.intent.action.MEDIA_MOUNTED系统权限,但是再Android 4.4系统以后,限制了只有系统应用才有使用广播通知系统扫描的权限,否则会抛出异常信息*/
// Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
// Uri uri = Uri.fromFile(file);
// intent.setData(uri);
// context.sendBroadcast(intent);
MediaScannerConnection.scanFile(context.getApplicationContext(), new String[]{filePath}, null,
new MediaScannerConnection.MediaScannerConnectionClient() {
@Override
public void onMediaScannerConnected() {
L.e("call: onMediaScannerConnected([])-> ");
}
@Override
public void onScanCompleted(String path, Uri uri) {
L.e("call: onScanCompleted([path, uri])-> " + path + " ->" + uri);
}
});
}
}
示例15: scanVideo
import android.media.MediaScannerConnection; //导入依赖的package包/类
public static void scanVideo(Context context, String videoPath) {
File file = new File(videoPath);
if (file.exists() && context != null) {
MediaScannerConnection.scanFile(context, new String[]{videoPath}, new String[]{"video/mp4"},
new MediaScannerConnection.MediaScannerConnectionClient() {
@Override
public void onMediaScannerConnected() {
L.e("call: onMediaScannerConnected([])-> ");
}
@Override
public void onScanCompleted(String path, Uri uri) {
L.e("call: onScanCompleted([path, uri])-> " + path + " ->" + uri);
}
});
}
}