如何将ImageView转换为文件imageFile

时间:2018-08-10 18:59:58

标签: android eclipse

如何将ImageView转换为文件imageFile

伙计们,

我的Android版本> 24有问题

我需要使用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 个答案:

答案 0 :(得分:1)

这是我用来从图像Uri制作文件的代码:

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

getRealPathFromUri函数:

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);
    }
}