用新创建的位图更新安卓图片库[英] update android image gallery with newly created bitmap

本文是小编为大家收集整理的关于用新创建的位图更新安卓图片库的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试将图像文件保存到外部存储.我可以将图片保存到SDCard,但它不会显示在Androids Gallery应用程序中.我尝试过这种方法:

File path = Environment.getExternalStorageDirectory();
            File f = new File(path + "/mydirectory/" + imageName + "_" +     System.currentTimeMillis() + ".jpg");
            FileOutputStream fos = new FileOutputStream(f);
            f.mkdirs();
            b.compress(CompressFormat.JPEG, 100, fos);
            fos.close();

            Uri contentUri = Uri.fromFile(f);
            Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
            mediaScanIntent.setData(contentUri);
            getApplicationContext().sendBroadcast(mediaScanIntent);

但它没有在画廊中显示.任何人都可以指出我的正确方向来解决这个问题吗?

推荐答案

使用此代码在Android设备库中保存图像位图

public void savePhoto(Bitmap bmp)
{
imageFileFolder = new File(Environment.getExternalStorageDirectory(),"Rotate");
imageFileFolder.mkdir();
FileOutputStream out = null;
Calendar c = Calendar.getInstance();
String date = fromInt(c.get(Calendar.MONTH))
            + fromInt(c.get(Calendar.DAY_OF_MONTH))
            + fromInt(c.get(Calendar.YEAR))
            + fromInt(c.get(Calendar.HOUR_OF_DAY))
            + fromInt(c.get(Calendar.MINUTE))
            + fromInt(c.get(Calendar.SECOND));
imageFileName = new File(imageFileFolder, date.toString() + ".jpg");
try
{
 out = new FileOutputStream(imageFileName);
 bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
 out.flush();
 out.close();
 scanPhoto(imageFileName.toString());
 out = null;
} catch (Exception e)
{
e.printStackTrace();
}
}


public String fromInt(int val)
{
return String.valueOf(val);
}


public void scanPhoto(final String imageFileName)
{
msConn = new MediaScannerConnection(PreviewDemo1.this,new MediaScannerConnectionClient()
{
public void onMediaScannerConnected()
{
msConn.scanFile(imageFileName, null);
Log.i("msClient obj  in Photo Utility","connection established");
}
public void onScanCompleted(String path, Uri uri)
{
msConn.disconnect();
Log.i("msClient obj in Photo Utility","scan completed");
}
});
msConn.connect();
} 

在这里,我在"旋转"文件夹中保存图像如果您不希望在Sevephoto方法中轻松更改它.

其他推荐答案

我知道我有点迟到,但是我猜对别人读到这件事的任何人都是对的,并且使用MediaScannerConnection是一种更新画廊的方法.至于Nevva的方式表明他的代码的变化是:

    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    getApplicationContext().sendBroadcast(mediaScanIntent);

其他推荐答案

我已经写下了关于扫描介质文件的详细帖子. http://androidyue.github.io/博客/2014/01/19/Scan-Media-Files-In-Android/ 我希望这可以帮助你.

本文地址:https://www.itbaoku.cn/post/102375.html

问题描述

I'm trying to save an image file to external storage. I can save the picture to the sdcard but it doesn't show up in Androids gallery application. I've tried this approach:

File path = Environment.getExternalStorageDirectory();
            File f = new File(path + "/mydirectory/" + imageName + "_" +     System.currentTimeMillis() + ".jpg");
            FileOutputStream fos = new FileOutputStream(f);
            f.mkdirs();
            b.compress(CompressFormat.JPEG, 100, fos);
            fos.close();

            Uri contentUri = Uri.fromFile(f);
            Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
            mediaScanIntent.setData(contentUri);
            getApplicationContext().sendBroadcast(mediaScanIntent);

But it doesn't show up in the gallery. Can anyone point me in the right direction to solve this problem?

推荐答案

Use this code to save an image Bitmap in android device gallery

public void savePhoto(Bitmap bmp)
{
imageFileFolder = new File(Environment.getExternalStorageDirectory(),"Rotate");
imageFileFolder.mkdir();
FileOutputStream out = null;
Calendar c = Calendar.getInstance();
String date = fromInt(c.get(Calendar.MONTH))
            + fromInt(c.get(Calendar.DAY_OF_MONTH))
            + fromInt(c.get(Calendar.YEAR))
            + fromInt(c.get(Calendar.HOUR_OF_DAY))
            + fromInt(c.get(Calendar.MINUTE))
            + fromInt(c.get(Calendar.SECOND));
imageFileName = new File(imageFileFolder, date.toString() + ".jpg");
try
{
 out = new FileOutputStream(imageFileName);
 bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
 out.flush();
 out.close();
 scanPhoto(imageFileName.toString());
 out = null;
} catch (Exception e)
{
e.printStackTrace();
}
}


public String fromInt(int val)
{
return String.valueOf(val);
}


public void scanPhoto(final String imageFileName)
{
msConn = new MediaScannerConnection(PreviewDemo1.this,new MediaScannerConnectionClient()
{
public void onMediaScannerConnected()
{
msConn.scanFile(imageFileName, null);
Log.i("msClient obj  in Photo Utility","connection established");
}
public void onScanCompleted(String path, Uri uri)
{
msConn.disconnect();
Log.i("msClient obj in Photo Utility","scan completed");
}
});
msConn.connect();
} 

Here i am saving the image in " Rotate " folder if you dont want that you can change it easily in savePhoto method.

其他推荐答案

I know i am a little late to answer this but i guess for anyone else reading this the answer given by surendra is right and that using the MediaScannerConnection is a way to update the gallery. As for the way nevva was suggesting the changes to his code are :

    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    getApplicationContext().sendBroadcast(mediaScanIntent);

其他推荐答案

I have written down a detailed post about scanning media files in Android. http://androidyue.github.io/blog/2014/01/19/scan-media-files-in-android/ I hope this could help you.