【问题标题】:view images in the phone gallery在手机图库中查看图像
【发布时间】:2018-01-21 12:08:17
【问题描述】:

当我保存图像时,我希望图像出现在画廊中,而不仅仅是在内部存储中,如应用程序壁纸、Facebook Messenger

我的代码,点击按钮

holder.img_download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                File dir = new File(Environment.getExternalStorageDirectory(), "/Wallpapers");
                if(!dir.exists()) {
                    dir.mkdirs();
                }

                Bitmap bitmap = ((BitmapDrawable)holder.img_photo.getDrawable()).getBitmap();
                saveImage(bitmap,dir);

            }


        });

函数保存图像

private void saveImage(Bitmap finalBitmap,File dir) {


        Random r = new Random();
        String fname = "Image_" + r.nextInt(1000000) + ".jpg";
        File file = new File(dir, fname);
        if (file.exists()) file.delete();
        try {
            FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
            Toast toasty = Toasty.success(context,"Saved", Toast.LENGTH_LONG);
            toasty.setGravity(Gravity.CENTER, 0, 0);
            toasty.show();
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + dir)));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

我保存图片没有任何问题,但我想像图像应用程序一样显示在图库中

【问题讨论】:

  • 图库是什么意思,是在相机默认目录中吗?
  • 图片未出现在图库中

标签: android android-studio bitmap imageview android-drawable


【解决方案1】:

试试这个在图库中添加图片:

public void addImageToGallery(final String filePath, final Context context) {

    ContentValues contentValues = new ContentValues();

    contentValues.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
    contentValues.put(Images.Media.MIME_TYPE, "image/jpeg");
    contentValues.put(MediaStore.MediaColumns.DATA, filePath);

    context.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, contentValues);
}

【讨论】:

    【解决方案2】:

    而不是 File dir = new File(Environment.getExternalStorageDirectory(), "/Wallpapers");

    使用

     File file = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), albumName);
    

    【讨论】:

    • 可以,但是有文件夹问题,只有删除内存中的其他文件才会出现,内存未满
    • 问题已解决,但只有删除任何图像且内存也为空时才会出现文件夹
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 2013-02-09
    • 2013-07-23
    • 1970-01-01
    相关资源
    最近更新 更多