【问题标题】:How to store background image from imageView in bitmap?如何将imageView中的背景图像存储在位图中?
【发布时间】:2017-12-11 11:11:47
【问题描述】:

我正在尝试将 imageView 中的图像存储在位图中,以便可以将其存储在 android 设备的图库中。每次我保存图像时,都不会存储 imageView 的背景。我错过了什么?

这是我的代码:

ImageView imageView = (ImageView) findViewById(R.id.img);
imageView.setBackgroundResource(R.drawable.img1);
BitmapDrawable draw = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = draw.getBitmap();

将图像存储到图库中的代码是:

FileOutputStream outStream = null;
            File dir = new File(
                    Environment
                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                    "MyAlbum");
            if (!dir.exists()) {
                if (!dir.mkdirs()) {
                    Log.d("MyAlbum", "failed to create directory");
                    Toast.makeText(MainActivity.this, "Failed to make directory", Toast.LENGTH_SHORT).show();
                }
            }
            String fileName = String.format("%d.jpg", System.currentTimeMillis());
            File outFile = new File(dir, fileName);
            try {
                outStream = new FileOutputStream(outFile);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
                outStream.flush();
                outStream.close();

                Toast.makeText(getApplicationContext(), "PICTURE SAVED", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                intent.setData(Uri.fromFile(dir));
                sendBroadcast(intent);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

【问题讨论】:

  • 你有适配器吗?你能分享相关的位置代码
  • 将图像存储到存储中的其余代码在哪里??
  • @ankitDemonstrate 你现在可以看到将图像存储到存储中的代码
  • 如果一切正常,您是否检查了 WRITE_STORAGE 的运行时权限?
  • 您想将 ImageView 的 src 和 ImageView 的背景合并成一个图像并保存?

标签: android android-imageview android-bitmap


【解决方案1】:

在这种情况下,您可以对该视图(ImageView)进行截图,它会简单地将此时在该视图上绘制的内容转换为您可以保存的位图。

答案已经提到here。

神奇之处在于

ImageView yourImageView = .. // Get reference it to your view.
yourImageView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(yourImageView.getDrawingCache());
yourImageView.setDrawingCacheEnabled(false);

你可以使用你的快照btimap。

【讨论】:

  • 当我截取 imageView 的屏幕截图时,两个图像的宽度和高度都不同。因此,高度较小的图像会拉伸并破坏图像。 @elmorabea
【解决方案2】:

你可以试试这个,

private void saveImageToStorage(Bitmap finalBitmap, String image_name) {

        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root);
        myDir.mkdirs();
        String fname = "Image-" + image_name+ ".jpg";
        File file = new File(myDir, fname);
        if (file.exists()) file.delete();
        Log.i("LOAD", root + fname);
        try {
            FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

编码愉快:-)

【讨论】:

  • 您的回答与问题完全不同
猜你喜欢
  • 1970-01-01
  • 2013-09-18
  • 1970-01-01
  • 1970-01-01
  • 2015-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-04
相关资源
最近更新 更多