简体   繁体   English

如何将位图图像转换为Uri

[英]How To Convert A Bitmap Image To Uri

I have not found the answer to this question anywhere. 我没有在任何地方找到这个问题的答案。

The Bitmap Image is processed in The application, meaning there is no File path to get the Image. 位图图像在应用程序中处理,这意味着没有文件路径来获取图像。

Below is how to convert a Uri to Bitmap 下面是如何将Uri转换为位图

 if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {
        Uri selectedImageUri = data.getData();

        imageview.setImageURI(selectedImageUri);

        try {
            bitmap1 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "" + e, Toast.LENGTH_SHORT).show();
        }

        bitmap1.compress(Bitmap.CompressFormat.JPEG, 7, bytearrayoutputstream);

        BYTE = bytearrayoutputstream.toByteArray();

        bitmap2 = BitmapFactory.decodeByteArray(BYTE, 0, BYTE.length);

        imagetoo.setImageBitmap(bitmap2);
    }

How do I now reconvert to a Uri 我现在如何重新转换为Uri

URI is super set of URL that means its a path to file . URI是一组超级URL,表示它是文件的路径。 whereas Bitmap is a digital image composed of a matrix of dots.Bitmap represents a data and uri represents that location where data is saved .SO if you need to get a URI for a bitmap You just need to save it on a storage . 而Bitmap是由点矩阵组成的数字图像.Bitmap表示数据,uri表示保存数据的位置。如果需要获取位图的URI,则只需将其保存在存储上即可。 In android you can do it by Java IO like below:First Create a file where you want to save it : 在android中你可以通过Java IO来完成它,如下所示:首先创建一个你想要保存它的文件:

public File createImageFile() {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File mFileTemp = null;
    String root=activity.getDir("my_sub_dir",Context.MODE_PRIVATE).getAbsolutePath();
    File myDir = new File(root + "/Img");
    if(!myDir.exists()){
        myDir.mkdirs();
    }
    try {
        mFileTemp=File.createTempFile(imageFileName,".jpg",myDir.getAbsoluteFile());
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return mFileTemp;
}

Then flush it and you will get the URi 然后冲洗它,你会得到URi

 File file = createImageFile(context);
 if (file != null) {
    FileOutputStream fout;
    try {
        fout = new FileOutputStream(file);
        currentImage.compress(Bitmap.CompressFormat.PNG, 70, fout);
        fout.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Uri uri=Uri.fromFile(file);
}

This is just an example not idle code for all android version. 这只是一个例子,不是所有Android版本的空闲代码。 To use Uri above and on android N you should use FileProvider to serve the file . 要在Android N上面和上面使用Uri ,你应该使用FileProvider来提供文件。 Follow the Commonsware's answer. 按照Commonsware的回答。

Use compress() on Bitmap to write the bitmap to a file. Bitmap使用compress()Bitmap写入文件。 Then, most likely, use FileProvider to serve that file, where getUriForFile() gives you the Uri corresponding to the file . 然后,很可能使用FileProvider来提供该文件,其中getUriForFile()为您提供与该文件对应的Uri

IOW, you do not "convert" a bitmap to a Uri . IOW,你不要将位图“转换”为Uri You save the bitmap somewhere that gives you a Uri . 你将位图保存在一个给你一个Uri地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM