如何获取位图的路径以在Uri中使用

huangapple 未分类评论45阅读模式
英文:

How to get the path of a Bitmap to use in Uri

问题

我正在尝试分享一张图片。为此,我需要位图的路径以便将其作为 Uri 发送。
我目前使用的代码行已经过时,并且根据 API 结果可能为 Null。

            String path = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Challengers", null);

获取位图路径的正确方法是什么,以便使用 Uri 将位图分享到其他应用程序?

提前感谢您的帮助。

英文:

I am trying to share an image. To do so i need the path of the bitmap to send it in Uri.
The current line i have is deprecated and depending on the API result Null.

            String path = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Challengers", null);

What is the proper way to get the path of a Bitmap to share a Bitmap to other app using Uri?

Thanks in advance for your help.

答案1

得分: 0

终于找到了问题的原因。我已经在清单文件中添加了权限,但还需要在onCreate方法中添加几行代码:

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, requestCode);
onRequestPermissionsResult(requestCode, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, grantResults);
英文:

Finally found what was causing problems. I had put permissions in the Manifest but also needed to add a couple lines in onCreate

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},requestCode);
    onRequestPermissionsResult(requestCode,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},grantResults);

答案2

得分: 0

这可能会有所帮助

public static Bitmap getBitmapFromURL(String imgUrl) {
    try {
        URL url = new URL(imgUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        // 记录异常
        return null;
    }
}
英文:

This might help

public static Bitmap getBitmapFromURL(String imgUrl) {
try {
    URL url = new URL(imgUrl);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);
    connection.connect();
    InputStream input = connection.getInputStream();
    Bitmap myBitmap = BitmapFactory.decodeStream(input);
    return myBitmap;
} catch (IOException e) {
    // Log exception
    return null;
}
}

答案3

得分: -1

以下是您可以尝试的代码以获取图像 URI:

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

You can try below code to get Image Uri,

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

huangapple
  • 本文由 发表于 2020年4月7日 11:58:57
  • 转载请务必保留本文链接:https://java.coder-hub.com/61072603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定