【问题标题】:How to save a bitmap image with imageview onclick [closed]如何使用imageview onclick保存位图图像[关闭]
【发布时间】:2023-03-23 13:38:01
【问题描述】:

我正在开发 android 中的图像着色。因此,当我单击另一个图像视图(如保存)时,将颜色应用于我的图像后,我必须将该图像保存到图库。

【问题讨论】:

    标签: android android-gallery


    【解决方案1】:

    imageView获取位图:

    imageview.buildDrawingCache();
    Bitmap bm=imageview.getDrawingCache();
    

    要将其保存在文件中:

    OutputStream fOut = null;
    Uri outputFileUri;
    try {
        File root = new File(Environment.getExternalStorageDirectory()
            + File.separator + "folder_name" + File.separator);
        root.mkdirs();
        File sdImageMainDirectory = new File(root, "myPicName.jpg");
        outputFileUri = Uri.fromFile(sdImageMainDirectory);
        fOut = new FileOutputStream(sdImageMainDirectory);
    } catch (Exception e) {
        Toast.makeText(this, "Error occured. Please try again later.",
        Toast.LENGTH_SHORT).show();
    }
    try {
        bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
    } catch (Exception e) {
    }
    

    【讨论】:

    • 谢谢让我试试这个......@Seshu Vinay
    • 嘿,我收到错误消息“发生错误。请稍后再试。”甚至无法看到根目录中创建的任何目录??
    • 在清单中添加权限:
    • 将此行添加到清单文件后,我也得到相同的错误 vinay???
    • @SeshuVinay 有用的答案,但你能告诉你为什么使用 outputFileUri 变量并分配该值,因为你没有使用它
    【解决方案2】:

    你必须

    1. 将图像保存到永久存储。
    2. MediaStore 内容提供程序添加一个条目。

    第一个可以使用以下代码实现:

    FileOutputStream out = new FileOutputStream(filePath);
    bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
    

    第二,

    MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, imagePath, name, description);
    

    【讨论】:

    • 谢谢让我试试这个......@Ragunath Jawahar
    【解决方案3】:

    先获取imageView的drawingCache(bitmap),然后将bitmap保存到SDCard中。

    文件夹 = new File(Environment.getExternalStorageDirectory()+"/folder/"); if(!folder.exists()) 文件夹Appointment.mkdirs();

    try {
        this.setDrawingCacheEnabled(true);
        FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+"/folder/file"));
        Bitmap bitmap = YOUR_IMAGE_VIEW.getDrawingCache();
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

    【讨论】:

    • 谢谢让我试试这个......@akkilis
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多