How can I transform a Bitmap into a Uri?

前端 未结 6 2080
一向
一向 2020-12-30 19:34

I\'m trying to share images with Facebook, twitter, etc using SHARE INTENT from Android.

I found code to send a image to the share intent, but this code

相关标签:
6条回答
  • 2020-12-30 19:53
    String picName = "pic.jpg";
            String PATH = Environment.getExternalStorageDirectory().getPath()+ picName;
            File f = new File(PATH);
            Uri yourUri = Uri.fromFile(f);
    
    0 讨论(0)
  • 2020-12-30 19:58

    Here is the Colin's Blog who suggest the simple method to convert bitmap to 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);
    }
    
    0 讨论(0)
  • 2020-12-30 20:02

    pass bitmap and compressFormat like (PNG, JPG, etc...) and image quality in percentage

    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);
    }
    
    0 讨论(0)
  • 2020-12-30 20:03

    Well you can't transforma a bitmap file into a uri. Read more about URI here

    URI is an Uniform Resource Identifier. But you can place the bitmap in an absolute or relative URI like this

    Absolute: http://android.com/yourImage.bmp
    Relative: yourImage.bmp 
    
    0 讨论(0)
  • 2020-12-30 20:04
    String FILENAME = "image.png";
    String PATH = "/mnt/sdcard/"+ FILENAME;
    File f = new File(PATH);
    Uri yourUri = Uri.fromFile(f);
    
    0 讨论(0)
  • 2020-12-30 20:13

    The above solution uses media store and stores the image in the users main image folder making it viewable through the gallery/photo viewer. This solution will store it as a temporary file in your apps data. In this example inImage is a Bitmap and title is a string for the name of the image file.

        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);
    
    0 讨论(0)
提交回复
热议问题