使用新创建的位图更新 Android 图片库

发布于 2024-12-08 16:31:51 字数 732 浏览 0 评论 0原文

我正在尝试将图像文件保存到外部存储。我可以将图片保存到 SD 卡,但它不会显示在 Android 画廊应用程序中。我尝试过这种方法:

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);

但它没有出现在画廊中。谁能指出我解决这个问题的正确方向?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怪我闹别瞎闹 2024-12-15 16:31:51

使用此代码在 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();
} 

图这里我将图像保存在“旋转”文件夹中,如果您不希望您可以在 savePhoto 方法中轻松更改它。

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.

嘿咻 2024-12-15 16:31:51

我知道我回答这个问题有点晚了,但我想对于阅读本文的其他人来说,surendra 给出的答案是正确的,并且使用 MediaScannerConnection 是更新图库的一种方法。至于 nevva 建议更改代码的方式是:

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

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);
迷途知返 2024-12-15 16:31:51

我写了一篇关于在 Android 中扫描媒体文件的详细文章。
http://androidyue.github.io/博客/2014/01/19/scan-media-files-in-android/
我希望这可以帮助你。

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.

以为你会在 2024-12-15 16:31:51

将位图保存到 SD 后,只需使用 MediaScannerConnection 即可:

MediaScannerConnection.scanFile(this,
                new String[] { Bitmapfile.getAbsolutePath() }, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        //now visible in gallery
                    }
                }
            );

Simply use MediaScannerConnection after you saved your bitmap to sd:

MediaScannerConnection.scanFile(this,
                new String[] { Bitmapfile.getAbsolutePath() }, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        //now visible in gallery
                    }
                }
            );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文