【问题标题】:Save image in the gallery将图像保存在图库中
【发布时间】:2017-05-26 17:05:34
【问题描述】:

我有一个问题,如何将位图从我的应用程序保存到 Button 内的 android 库中?我只想将我的位图放在画廊中,用户可以使用它来将其设置为他的背景。那么进展如何?

【问题讨论】:

  • 或许您应该尝试将图像保存在设备位置,Gallery 会从中读取图像。例如 DCIM 或类似的东西。
  • 画廊是一个应用程序,而不是一个地方。您将图像存储在一个地方,例如external storage
  • 好的,我该如何处理?我尝试了这么多版本,但没有一个对我有用?保存起来听起来很简单

标签: android image file save gallery


【解决方案1】:

您可以像这样将图像保存在图库中。

MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);

这样试试

图像显示在存储在媒体存储提供商上的图库中

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

ContentValues values = new ContentValues();

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

context.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);

}

【讨论】:

    【解决方案2】:

    您可以修改以下函数以满足您的需要

     private void takeSnapShot(Bitmap bitmap) {
        OutputStream outStream = null;
        String state = Environment.getExternalStorageState();
        String appName = mContext.getString(R.string.app_name);
    
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            String sdroot = Environment.getExternalStorageDirectory().getAbsolutePath();
            String path = sdroot + "/" + appName;
            File dir = new File(path);
            if (!dir.exists() && !dir.mkdirs()) {
                return;
            }
    
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US);
            String dstr = sdf.format(new Date());
            String filename = path + "/" + dstr + ".jpg";
    
            try {
                outStream = new FileOutputStream(filename);
            } catch (FileNotFoundException e) {
                LogUtils.println("Exception while writing image");
                e.printStackTrace();
            }
    
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
    
            try {
                if (outStream != null)
                    outStream.close();
                mMediaScanner.scanMediaByName(filename, R.string.snapshot_save);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    
        } else {
            //  Add some parameters to the image that will be stored in the Image ContentProvider
            int UNIQUE_BUCKET_ID = 111;
            String disp = "anything";
    
            ContentValues values = new ContentValues(7);
            values.put(MediaStore.Images.Media.DISPLAY_NAME, disp);
            values.put(MediaStore.Images.Media.TITLE, disp);
            values.put(MediaStore.Images.Media.DESCRIPTION, appName + disp);
            values.put(MediaStore.Images.Media.BUCKET_DISPLAY_NAME, appName + " snapshot");
            values.put(MediaStore.Images.Media.BUCKET_ID, UNIQUE_BUCKET_ID);
            values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
            values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
            // Inserting the image meta data inside the content provider
            Uri uri = mContext.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    
            try {
                if (uri != null) {
                    outStream = mContext.getContentResolver().openOutputStream(uri);
                    if (outStream != null) {
                        outStream.close();
                    }
                }
            } catch (FileNotFoundException e) {
                LogUtils.println("Exception while writing image");
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      相关资源
      最近更新 更多