代码之家  ›  专栏  ›  技术社区  ›  Will

如何将ImageView转换为文件imageFile

  •  0
  • Will  · 技术社区  · 6 年前

    伙计们,

    我需要使用FileProvider,但是,我的图像在ImageView中

    我需要获取imageview图像,然后使用文件提供程序,然后我将能够使用该图像。

    ImageView ivImage = (ImageView) findViewById(R.id.imgFullscreen);
    
        if (Build.VERSION.SDK_INT >= 24)
        { //I'm working here
            Uri bmpUri =
               FileProvider.getUriForFile (this, "com.xxxxx.fileprovider", newfile);
    
        }else
        {
           Uri bmpUri = getLocalBitmapUri(ivImage);
        }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   ThomasLothbrok    6 年前

    下面是我用来从图像Uri生成文件的代码:

    Uri uri = FileProvider.getUriForFile (this, "com.xxxxx.fileprovider", newfile);
    String filepath = getRealPathFromURI(uri, mActivity);
    File file = new File(filepath);
    

    private String getRealPathFromURI(Uri contentURI, Activity activity) {
        Cursor cursor = activity.getContentResolver().query(contentURI, null, null, null, null);
        if (cursor == null) {
            return contentURI.getPath();
        } else {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            return cursor.getString(idx);
        }
    }