Android 如何在多媒体资料中保存照片

Android 如何在多媒体资料中保存照片,android,photo-gallery,Android,Photo Gallery,您好,我在手机屏幕上使用ss应用程序,但此应用程序将所有照片存储在内部存储器中的本地文件中,并且当我在手机中打开我的图库时,没有显示所有照片 那么,如何将每张照片存储在图库文件夹中呢 我拿着SS private void takeAScreenshot() { Date now = new Date(); android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now); try { Str

您好,我在手机屏幕上使用ss应用程序,但此应用程序将所有照片存储在内部存储器中的本地文件中,并且当我在手机中打开我的图库时,没有显示所有照片

那么,如何将每张照片存储在图库文件夹中呢

我拿着SS

private void takeAScreenshot() {
    Date now = new Date();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
    try {
        String mPath = Environment.getExternalStorageDirectory().toString()
                + "/" + now + ".jpg";
        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);
        File imageFile = new File(mPath);
        FileOutputStream outputStream = new FileOutputStream(imageFile);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        outputStream.flush();
        outputStream.close();
    } catch (Throwable e) {
        e.printStackTrace();
    }
}
我检查权限

我还可以找到从aparat保存文件的位置:

/存储/仿真/0/DCIM/Camera/img_name.jpg

我发现这很有效:

ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
            values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
            values.put(MediaStore.MediaColumns.DATA, mPath)
但是图像保存在库文件夹和内部存储中,请帮助添加以下内容:

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

填写位图、标题和描述的详细信息,或者将其保留为。

保存后使用MediaScanner

private void takeAScreenshot() {
        Date now = new Date();
        android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
        try {
            String mPath = Environment.getExternalStorageDirectory().toString()
                    + "/" + now + ".jpg";
            View v1 = getWindow().getDecorView().getRootView();
            v1.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
            v1.setDrawingCacheEnabled(false);
            File imageFile = new File(mPath);
            FileOutputStream outputStream = new FileOutputStream(imageFile);
            int quality = 100;
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        MediaScannerConnection.scanFile(ActivityName.this,
                new String[]{imageFile.toString()}, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(final String path, final Uri uri) {
                        ActivityName.this.runOnUiThread(new Runnable() {
                            public void run() {
                                Intent shareIntent = new Intent();
                                shareIntent.setAction(Intent.ACTION_SEND);
                                shareIntent.putExtra(Intent.EXTRA_STREAM,
                                        uri);
                                shareIntent.setType("image/jpeg");
                                startActivity(Intent.createChooser(shareIntent,
                                        "choose one"));
                            }
                        });
                    }
                });
    }

检查,这是很好的升级我的应用程序,请检查我的编辑回答我有一个问题,照片保存在2个位置一个文件保存一个文件路径创建。第二个原因是您正在使用内容解析程序将该图像放入图像数据库,并创建第二个副本。如果这有帮助,您可以选择mark或vote up或两者都满意的编码:这样我就可以使该文件仅保存在图像数据库中?不要将其保存到db MediaScannerConnection代码将为您执行此操作。