【问题标题】:How can I transform a Bitmap into a Uri?如何将位图转换为 Uri?
【发布时间】:2012-01-07 21:47:30
【问题描述】:

我正在尝试使用来自 Android 的 SHARE INTENT 与 Facebook、Twitter 等分享图像。

我找到了将图像发送到共享意图的代码,但此代码需要位图的 URI:fullSizeImageUri

这是完整的代码:

private void startShareMediaActivity(Bitmap image) {
    boolean isVideo=false;
    String mimeType="bmp";
    Uri fullSizeImageUri=null;
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.setType(mimeType);
    intent.putExtra(Intent.EXTRA_STREAM, fullSizeImageUri);
    try {
        startActivity(Intent.createChooser(intent, (isVideo ? "video" : "image")));
    } catch (android.content.ActivityNotFoundException ex) { }
}

如何将 Bitmap 转换为 Uri?

【问题讨论】:

    标签: android bitmap uri


    【解决方案1】:

    这是 Colin 的博客,他提出了将位图转换为 Uri 的简单方法Click here

    public Uri getImageUri(Context inContext, Bitmap inImage) {
      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
      inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
      String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
      return Uri.parse(path);
    }
    

    【讨论】:

    • 请注意,这会创建一个将保留在用户图库中的图像。
    • 救了我的命。谢谢!
    • path 可以为空,如果是,Uri.parse(path) 会使应用崩溃
    • 别忘了获得 WRITE_EXTERNAL_STORAGE 权限。否则 insertImage 方法可能返回 null。请参阅:stackoverflow.com/a/35283353/3341089
    • 在 android oreo 中不起作用,String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);路径将变为空。
    【解决方案2】:
    String FILENAME = "image.png";
    String PATH = "/mnt/sdcard/"+ FILENAME;
    File f = new File(PATH);
    Uri yourUri = Uri.fromFile(f);
    

    【讨论】:

    • Parse 总是将字符串而不是文件作为参数。下次阅读有关功能的详细信息或进行测试。
    • 我们不能在不保存位图的情况下获取 Uri
    【解决方案3】:

    上述解决方案使用媒体存储并将图像存储在用户主图像文件夹中,使其可通过图库/照片查看器查看。此解决方案会将其作为临时文件存储在您的应用数据中。在这个例子中,inImage 是一个位图,title 是一个字符串,表示图像文件的名称。

        File tempDir= Environment.getExternalStorageDirectory();
        tempDir=new File(tempDir.getAbsolutePath()+"/.temp/");
        tempDir.mkdir();
        File tempFile = File.createTempFile(title, ".jpg", tempDir);
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        byte[] bitmapData = bytes.toByteArray();
    
        //write the bytes in file
        FileOutputStream fos = new FileOutputStream(tempFile);
        fos.write(bitmapData);
        fos.flush();
        fos.close();
        return Uri.fromFile(tempFile);
    

    【讨论】:

    • 临时有多临时?什么时候删除文件?我可以快速连续对多张图像使用相同的文件名吗?
    • 而不是 tempDir=new File(tempDir.getAbsolutePath()+"/.temp/");您始终可以使用您的应用程序专用的目录..并将图像保留在那里
    • @Joel 此外,如果您在循环名称中使用文件名“file”+i 并且在您的过程成功(如 api 响应)时,您可以清除此文件夹 -> 必须是手动完成
    【解决方案4】:

    传递位图和压缩格式,例如(PNG、JPG 等...)和图像质量百分比

    public Uri getImageUri(Bitmap src, Bitmap.CompressFormat format, int quality) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        src.compress(format, quality, os);
    
        String path = MediaStore.Images.Media.insertImage(getContentResolver(), src, "title", null);
        return Uri.parse(path);
    }
    

    【讨论】:

    • path 可以为 null,因此您的代码会在 Uri.parse(path) 上崩溃
    • 你检查路径是否为空。在你经过 Uri 之后
    • @PankajTalaviya 此方法将图像保存为 .jpg 格式,即使我们提供 PNG 格式进行压缩。
    • @PankajMundra 增加降低质量百分比以压缩图像
    • 所以根据你的说法,将图像保存为 .png 的适当百分比应该是多少......因为如果它保存为 jpeg,则图像的背景是黑色的。
    【解决方案5】:

    您不能将位图文件转换为 uri。阅读更多关于 URI here

    URI 是一个统一资源标识符。但是您可以像这样将位图放置在绝对或相对 URI 中

    Absolute: http://android.com/yourImage.bmp
    Relative: yourImage.bmp 
    

    【讨论】:

    • 那怎样才能实现我的需求呢?这不可能?
    • 我猜你必须重新考虑你的方式才能看到这个......检查 Facebook 和 twitter APIS 看看你如何实现图像共享
    【解决方案6】:
    String picName = "pic.jpg";
            String PATH = Environment.getExternalStorageDirectory().getPath()+ picName;
            File f = new File(PATH);
            Uri yourUri = Uri.fromFile(f);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      • 2018-07-21
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      相关资源
      最近更新 更多