Will
Will

Reputation: 231

How to Convert ImageView to File imageFile

How to Convert ImageView to File imageFile

Guys,

I have a problem with the Android versions > 24

I need to use FileProvider, however, my image is in an ImageView

I need to get the imageview image, then use the file-provider and then I will be able to use the image.

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

Upvotes: 0

Views: 97

Answers (1)

ThomasLothbrok
ThomasLothbrok

Reputation: 76

Here's the code that I use to make a File from image Uri:

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

getRealPathFromUri function:

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

Upvotes: 1

Related Questions