Android 下载图像,保存到存储,但图像库无法看到该图像
我正在制作gallery应用程序,它从服务器下载图像并放入应用程序文件夹。但是当我打开这个应用程序()时,我看不到我下载的图像。这是我的密码 下载图片:Android 下载图像,保存到存储,但图像库无法看到该图像,android,image,bitmap,save,gallery,Android,Image,Bitmap,Save,Gallery,我正在制作gallery应用程序,它从服务器下载图像并放入应用程序文件夹。但是当我打开这个应用程序()时,我看不到我下载的图像。这是我的密码 下载图片: Glide.with(getActivity()).load(path) .asBitmap() .diskCacheStrategy(DiskCacheStrategy.ALL) .into(new SimpleTarget
Glide.with(getActivity()).load(path)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(final Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
Log.d(TAG, "pamti se");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
saveToDirectory(resource, path);
}
}, 0);
}
});
第一步
将文件保存到Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY\u PICTURES)
第二步。
使用MediaScanner通知系统有关新文件/映像的信息。我为此创建了一个助手类。您只需传入文件路径:
import android.content.Context;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.lang.ref.WeakReference;
public class MediaScannerUtils implements
MediaScannerConnection.MediaScannerConnectionClient {
private static final String LOG_TAG = "MediaScannerUtils";
private MediaScannerConnection mConnection;
private @Nullable WeakReference<MediaScannerListener> mListenerRef;
private String mPathToScan;
private String mMimeType;
/**
* Initialize the MediaScannerUtils class
* @param context Any context
* @param listener A listener that gets notified when the scan is completed
* @param pathToScan The path to scan. If null, the entire sdcard is scanned
* @param mimeType The mimeType of files to look for. If null, defaults to "audio/*"
*/
public MediaScannerUtils(@NonNull Context context, @Nullable MediaScannerListener listener,
@Nullable String pathToScan, @Nullable String mimeType) {
LogUtils.d(LOG_TAG, "Initializing MediaScannerUtils");
mPathToScan = pathToScan;
mMimeType = mimeType;
if(pathToScan == null) {
//If path to scan is null, set path to the entire SdCard
mPathToScan = FileUtils.getSDCardAbsolutePath();
}
if(mimeType == null) {
//If mime type is null, set mime type to audio wildcard
mMimeType = "image/*";
}
if(listener != null) {
mListenerRef = new WeakReference<>(listener);
}
mConnection = new MediaScannerConnection(context.getApplicationContext(), this);
mConnection.connect();
}
/**
* Called to notify the client when a connection to the
* MediaScanner service has been established.
*/
public void onMediaScannerConnected() {
mConnection.scanFile(mPathToScan, mMimeType);
}
/**
* Called to notify the client when the media scanner has finished
* scanning a file.
* @param path the path to the file that has been scanned.
* @param uri the Uri for the file if the scanning operation succeeded
* and the file was added to the media database, or null if scanning failed.
*/
public void onScanCompleted(String path, Uri uri) {
if(mConnection != null) {
mConnection.disconnect();
}
if(mListenerRef != null) {
MediaScannerListener listener = mListenerRef.get();
if (listener != null) {
listener.onScanCompleted();
}
}
}
/**
* Disconnects the MediaScannerConnection and releases any heavy object
*/
public void release() {
if (mConnection != null) {
mConnection.disconnect();
mConnection = null;
}
}
public interface MediaScannerListener {
void onScanCompleted();
}
}
导入android.content.Context;
导入android.media.MediaScannerConnection;
导入android.net.Uri;
导入android.support.annotation.NonNull;
导入android.support.annotation.Nullable;
导入java.lang.ref.WeakReference;
公共类MediaScannerUtils实现
MediaScannerConnection.MediaScannerConnectionClient{
私有静态最终字符串日志\u TAG=“MediaScannerUtils”;
专用MediaScannerConnection mConnection;
private@Nullable WeakReference mListenerRef;
私有字符串mPathToScan;
私有字符串mMimeType;
/**
*初始化MediaScannerUtils类
*@param context任意上下文
*@param listener扫描完成时收到通知的侦听器
*@param pathToScan扫描路径。如果为空,则扫描整个SD卡
*@param mimeType要查找的文件的mimeType。如果为null,则默认为“audio/*”
*/
公共MediaScannerUrtils(@NonNull上下文,@Nullable MediaScannerListener listener,
@可空字符串路径扫描,@Nullable String mimeType){
LogUtils.d(LOG_标签,“初始化MediaScannerUtils”);
mPathToScan=路径扫描;
mMimeType=mimeType;
if(pathToScan==null){
//如果扫描路径为空,则将路径设置为整个SD卡
mPathToScan=FileUtils.getSDCardAbsolutePath();
}
if(mimeType==null){
//如果mime类型为null,请将mime类型设置为音频通配符
mMimeType=“image/*”;
}
if(侦听器!=null){
mListenerRef=新的WeakReference(侦听器);
}
mConnection=newMediaScannerConnection(context.getApplicationContext(),this);
mConnection.connect();
}
/**
*调用以在连接到
*MediaScanner服务已建立。
*/
MediaScannerconnected()上的公共无效{
mConnection.scanFile(mPathToScan,mMimeType);
}
/**
*调用以在媒体扫描程序完成时通知客户端
*扫描文件。
*@param path已扫描文件的路径。
*@param uri扫描操作成功时文件的uri
*并且该文件已添加到媒体数据库,如果扫描失败,则为null。
*/
已完成的公共void(字符串路径,Uri){
如果(mConnection!=null){
mConnection.disconnect();
}
if(mListenerRef!=null){
MediaScannerListener=mListenerRef.get();
if(侦听器!=null){
listener.onScanCompleted();
}
}
}
/**
*断开MediaScannerConnection并释放任何重物
*/
公开无效释放(){
如果(mConnection!=null){
mConnection.disconnect();
mConnection=null;
}
}
公共接口MediaScannerListener{
void onScanCompleted();
}
}
如果要将图像保存到内部应用程序存储中,则需要将图像保存到gallery(应用程序的外部存储)中,以便在应用程序外部可用
尝试以下方法:
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
//change mCurrentPhotoPath for your imagepath
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
参考文件:
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
//change mCurrentPhotoPath for your imagepath
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}