【问题标题】:How to save a bitmap in the gallery?如何在图库中保存位图?
【发布时间】:2017-05-25 11:58:49
【问题描述】:

所以我有一个位图,在我编辑它之后,我希望用户可以保存它并在画廊中看到它。我在谷歌搜索并发现了很多想法,但我尝试的所有方法都没有奏效。这是我的代码:

        ((ImageButton) findViewById(R.id. imageButton)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
            File myDir = new File(root + "/saved_images");
            myDir.mkdirs();
            Random generator = new Random();
            int n = 10000;
            n = generator.nextInt(n);
            String fname = "Image-" + n + ".jpg";
            File file = new File(myDir, fname);
            if (file.exists())
                file.delete();
            try {
                FileOutputStream out = new FileOutputStream(file);
                mutableBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                out.flush();
                out.close();
            }
            catch (Exception e) {
                e.printStackTrace();
            }


            // Tell the media scanner about the new file so that it is
            // immediately available to the user.
            MediaScannerConnection.scanFile(MainActivity7.this, new String[] { file.toString() }, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        public void onScanCompleted(String path, Uri uri) {
                            Log.i("ExternalStorage", "Scanned " + path + ":");
                            Log.i("ExternalStorage", "-> uri=" + uri);
                        }
                    });
        }
    });

那么有什么问题或者您有其他解决方案如何将我的位图保存在图库中?

【问题讨论】:

  • 当您检查 LogCat 时,您是否看到来自您的应用程序的堆栈跟踪?

标签: java android bitmap gallery


【解决方案1】:

进入设置>应用,找到你的应用是否允许存储权限。如果没有,请允许并重试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 2014-09-16
    相关资源
    最近更新 更多